Magento – Magento 2: How to show admin system configuration options conditionally

custommagento-2.1.3magento2system-configsystem.xml

I have a dropdown in Magento 2 config setting. Now I want the setting to get displayed only when there are any options available for the dropdown, else it should be not shown. I also want to display a message in this case that "NO options are available currently". How can I implement that without breaking magento2 standards.

Best Answer

Go to Admin -> Stores -> Configuration -> Advanced -> Admin -> Admin Base URL

Set Use Custom Admin URL to "Yes" & below it's code

magento\vendor\magento\module-backend\etc\adminhtml\system.xml

<group id="url" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Admin Base URL</label>
                <field id="use_custom" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
                    <label>Use Custom Admin URL</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <backend_model>Magento\Config\Model\Config\Backend\Admin\Usecustom</backend_model>
                </field>
                <field id="custom" translate="label comment" type="text" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
                    <label>Custom Admin URL</label>
                    <backend_model>Magento\Config\Model\Config\Backend\Admin\Custom</backend_model>
                    <depends>
                        <field id="use_custom">1</field>
                    </depends>
                    <comment>Make sure that base URL ends with '/' (slash), e.g. http://yourdomain/magento/</comment>
                </field>
                <field id="use_custom_path" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
                    <label>Use Custom Admin Path</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <backend_model>Magento\Config\Model\Config\Backend\Admin\Custompath</backend_model>
                </field>
                <field id="custom_path" translate="label comment" type="text" sortOrder="4" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
                    <label>Custom Admin Path</label>
                    <validate>required-entry validate-alphanum</validate>
                    <backend_model>Magento\Config\Model\Config\Backend\Admin\Custompath</backend_model>
                    <depends>
                        <field id="use_custom_path">1</field>
                    </depends>
                    <comment>You will have to sign in after you save your custom admin path.</comment>
                </field>
            </group>

Here

 <depends>
    <field id="use_custom_path">1</field>
 </depends>

1 = Yes, 0 = No.

It depends on what select value you set on your dropdown.

Related Topic