Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Disable Admin Menu item by the configuration value in Magento 2

    Here we learn in Magento 2- How we can admin menu items depends on the configuration value.
    If you want to create the custom module configuration in the configuration section, you can check the wonderful blog from here.

    1) Create System.xml file which is used for the configuration which is located in etc/adminhtml folder of the module.

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
        <system>
            <tab id="webkul" translate="label" sortOrder="10">
                <label>Webkul</label>
            </tab>
            <section id="test" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Test Configuration</label>
                <tab>webkul</tab>
                <resource>Webkul_Test::config_test</resource>
                <group id="settings" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Parameters</label>
                    <field id="enable" translate="label comment" sortOrder="1" type="select" showInDefault="1" >
                        <label>Enable</label>
                        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    </field>
                </group>
            </section>
        </system>
    </config>

    2) When you’re adding custom menu items in the admin, you can depend these on the settings in the configuration.
    Create Menu.xml file for the admin menu items which is also located in etc/adminhtml folder of the module.

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
        <menu>
        	<add id="Webkul_Test::manager" title="Test Manager" module="Webkul_Test" sortOrder="10" resource="Webkul_Test::manager"/>
            <add id="Webkul_Test::test" title="Test Menu" module="Webkul_Test" sortOrder="1" parent="Webkul_Test::manager" action="test/test/index" resource="Webkul_Test::test" dependsOnConfig="test/settings/enable"/>
        </menu>
    </config>
    

    In the add tag, there is an attribute dependsOnConfig which depends on the configuration filed’s value.
    If Enable filed value is yes then “Test Menu” will show otherwise menu item will not show in the module menus.

    If you want to custom module admin menu, you can check the blog from here.

    Hope it will help you. Thank you.

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top