How to Implement Field Dependency from Different Groups in Magento 2 System.xml
The system.xml is the main file responsible for any custom fields in the system configuration of Magento 2 admin. In order to add custom fields, tabs, groups, or performing any kind of customization in system configuration, one needs to create system.xml configuration in Magento 2.
Especially while creating Magento 2 extensions, we often require adding such kinds of fields in system configuration.
Sometimes, other fields have a dependency on one field. If the extension is enabled, then it should show other options to configure from the same group. However, what if you need to display fields in admin configuration depending on other fields from different groups?
For example, we have two groups: one is for the detailed configuration of API providers, and the other is for the API provider selection option. The required details for any API provider differ from APIs to APIs. In that case, implement field dependency from different groups in Magento 2 system.xml based on the selection of API providers.
Implement the below code whenever you need to set dependency from different groups in system.xml.
Method to Implement Field Dependency from Different Groups in Magento 2 System.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <system> <tab id="meetanshi" translate="label" class="meetanshi" sortOrder="100"> <label></label> </tab> <section id="module" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <group id="main" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Settings</label> <field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Custom Module</label> <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model> </field> </group> <group id="test" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Settings</label> <field id="var" translate="label" type="textarea" sortOrder="44" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Custom Field</label> <depends> <field id="module/main/enabled">1</field> </depends> </field> </group> </section> </system> </config> |
Any doubts about the above method can be mentioned in the Comments section below.
I’d be happy to help.
Also, do share the solution with Magento 2 store admins via social media.
Thanks.
2 Comments
Is there any way to setup 2 modules are dependent each other, if we install module A then error should be install module B or anything, if we install module B then error like pls install module A.
How do I make that possible?
When we install module from app code at that time also have to give error msg.
please give me suggestion or share any example you have.
Thank You. (~!~)
Hello Pravin,
No, it is not possible to do with manual (app/code) installation.
You can add the dependency if you develop module with composer so that the same scenario can be possible with the composer installation.
Thank You