The system.xml is a configuration file which is used to create configuration fields in Magento 2 System Configuration. You will need this if your module has some settings which the admin needs to set. You can go to Store -> Setting -> Configuration
to check how it look like.
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?
Implement the below code whenever you need to set dependency from different groups in system.xml.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <tab id="dolphintab" translate="label" sortOrder="100"> <label>Dolphin</label> </tab> <section id="setting" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <class>separator-top</class> <label>Settings</label> <tab>dolphintab</tab> <resource>Dolphin_InvoiceEmail::config_extension</resource> <group id="custom" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Custom Field</label> <field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Field</label> <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model> </field> <field id="var" translate="label" type="textarea" sortOrder="44" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Dependency Field</label> <if_module_enabled>Dolphin_Walletrewardpoints</if_module_enabled> <depends><field id="enabled">1</field></depends> </field> </group> <group id="credit_usages" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1"> <depends><field id="setting/custom/enabled">1</field></depends> <!-- This field will only be visible if the field with the id A_UNIQUE_FIELD_ID is set to value 1 --> <label>Credit Usages</label> <field id="credit" translate="label" type="text" sortOrder="44" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Use Credit</label> </field> </group> </section> </system> </config>
Field node reference
A <field>-Tag can have the following children:
Result:
After you implement the above code, check output in the admin panel of Magento 2. The dependency fields will be displayed in the system configuration.
I Hope, This instruction will be helpful for you.
If you have any difficulties regarding this blog, do consider them posting in the Comments section below!
I’m here to help.
Thank you!