Adding menu items

Menus are UI components that make it easy for users to find and navigate to specific or even undiscovered content and features in the store. Building on the previous tutorial, you will add a menu item to the Admin menu.

For a more in-depth look at menus, see Menus.

Adding a menu

First, you create the AdminMenu.cs class in the root folder of your module.

namespace MyOrg.HelloWorld
{
    public class AdminMenu
    {
    }
}

Implement the AdminMenuProvider

This class inherits from the abstract base class AdminMenuProvider and overrides the BuildMenuCore method.

public class AdminMenu : AdminMenuProvider
{
    protected override void BuildMenuCore(TreeNode<MenuItem> modulesNode)
    {
    }
}

Create a menu item

Next, you create a MenuItem using the ToBuilder() method.

  • ResKey is a reference to the resource in your localization files.

  • Icon adds a menu icon.

  • Action creates a route to your action, defined in the specified controller.

    This menu will simply point to your Configure method from the HelloWorldAdminController.

Add a localization string

Add a new resource to your localization to display the menu text.

Create tree nodes

Create a TreeNode menuNode from myMenuItem.

Get a reference node from modulesNode. This example uses the menu id settings.

For more menu items:

  • Open the Smartstore Admin page

  • Right-click on the desired menu

  • Select the Inspector

  • Search for "data-id"

Insert menuNode after refNode.

Now you should see a menu item in the admin configuration menu. Your code should look something like this:

Adding a submenu

Adding a submenu is very easy. First, you add a menu, just as you did above. The only difference is that you don't give the menu item an action.

Then create another menu item and specify the action.

Add localization strings

Add two new resources to your localization.

Create tree nodes

Again, create a TreeNode for each menu item.

Then use menuNode to insert and append the new menu items.

Your final code should look like this:

Conclusion

In this tutorial, you created a menu item, added it to the Admin menu, and added a submenu.

The code for this tutorial can be found in the examples repository.

Last updated

Was this helpful?