Creating Sub-Menus Under Customer Menu in Magento
Creating sub-menus under the Customer menu in Magento requires a slightly different approach compared to adding them under the Catalog or other main menus. The Magento system manages access permissions differently for these top-level menus.
Understanding the Structure of Magento XML Menus
Every menu item in Magento is defined within an <adminhtml> node in the config.xml file located in your module’s directory. The structure typically looks like this:
<menu>
<customers>
<title>Customers</title>
<sort_order>100</sort_order>
<children>
<!-- Child menu items go here -->
</children>
</customers>
</menu>
Creating the Sub-Menu Entry
To create a sub-menu under Customers, you need to add a new child node within the <children> section of the customers node. The structure for your sub-menu item should look like this:
<menu>
<customers>
<title>Customers</title>
<sort_order>100</sort_order>
<children>
<sub_menu_item>
<title>Sub Menu Item</title>
<sort_order>100</sort_order>
<action>adminhtml/adminhtml_submenuitem</action>
</sub_menu_item>
</children>
</customers>
</menu>
Replace sub_menu_item with the actual identifier for your sub-menu, and update the title, sort_order, and action attributes as needed.
Assigning Permissions
Unlike other menu items, adding a sub-menu under Customers requires you to assign permissions explicitly. This is because Magento has specific role-based access control for customer-related features.
You’ll need to define the new permission in your module’s etc/acl.xml file:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Customer">
<resource id="Magento_Customer::sub_menu_item">
<title>Sub Menu Item</title>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
Replace Magento_Customer::sub_menu_item with the actual resource identifier corresponding to your sub-menu item.
Testing Your New Sub-Menu
After updating these files, clear Magento’s cache and refresh the admin panel. You should now see your new sub-menu item listed under the Customers menu in the navigation bar on the left side of the screen.
Remember, for the sub-menu to be accessible, the currently logged-in user must have the appropriate role assigned with the necessary permissions defined in the etc/acl.xml file.
Conclusion
Creating a sub-menu under the Customer menu in Magento requires understanding how the system manages access control and how to define new resources. By following this guide, you can easily add customer-related sub-menus that appear seamlessly within the admin panel’s navigation structure.
For more detailed information on Magento XML menus and permissions management, I recommend checking out this comprehensive guide from Laravel Company.