Magento 2 Hide Admin Menu Based on System Config

If you want to hide admin menu based on system configuration in Magento 2, you can use the following code snippet:

This feature is very essential when we are creating a module with multiple features and have options to enable or disable some developed features. To show menu in admin menu section, we have to write the code in menu.xml file which is in Magemeta/HideAdminMenu/etc/adminhtml folder.

<?xml version="1.0"?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <add id="Magemeta_HideAdminMenu::mainmenu" title="Main Menu" module="Magemeta_HideAdminMenu" sortOrder="10" resource="Magemeta_HideAdminMenu::mainmenu"/>
        <add id="Magemeta_HideAdminMenu::menu" title="Menu" module="Magemeta_HideAdminMenu" sortOrder="10" parent="Magemeta_HideAdminMenu::mainmenu" dependsOnModule="Magemeta_HideAdminMenu" resource="Magemeta_HideAdminMenu::mainmenu"/>
        <add id="Magemeta_HideAdminMenu::submenu" title="Sub Menu" module="Magemeta_HideAdminMenu" sortOrder="1" parent="Magemeta_HideAdminMenu::menu" action="routename" resource="Magemeta_HideAdminMenu::submenu" dependsOnConfig="section/group/field"/>
    </menu>
</config>

With the attribute dependsOnModule we can manage visibility of the menu based on availability on any module. And with attribute dependsOnConfig we can manage visibility of the menu based on system config field.

Leave a Reply