Magento 2: system.xml in a custom module

Can I add a system.xml file for module configuration in Magento 2? If so, how?

+6
source share
1 answer

Yes, in magento 2 you can create a system configuration file that is the same as Magento 1.x. But he will need to create other files.

You must use the following file to create it.

 1) app/code/Vendor/Helloworld/etc/adminhtml/system.xml 2) app/code/Vendor/Helloworld/etc/acl.xml 

These 2 files are important for creating a system configuration.

In the system.xml file

Adding shared content

 <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd"> <system> <!-- Add new Tab --> <tab id="vendor" translate="label" sortOrder="300"> <label>Vendor Extension</label> </tab> <section id="helloworld" translate="label" type="text" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Helloworld</label> <tab>vendor</tab> <!-- resource tag name which we have to defined in the acl.xml --> <resource>Vendor_Helloworld::config_helloworld</resource> <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <label>General Options</label> <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Enabled</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> </group> </section> </system> </config> 

In acl.xml file

The file must contain the following content

 <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Acl/etc/acl.xsd"> <acl> <resources> <resource id="Magento_Backend::admin"> <resource id="Magento_Backend::stores"> <resource id="Magento_Backend::stores_settings"> <resource id="Magento_Config::config"> <!-- this resource id we can use in system.xml for section --> <resource id="Vendor_Helloworld::config_helloworld" title="Helloworld Section" sortOrder="80" /> </resource> </resource> </resource> </resource> </resources> </acl> </config> 

After that, clear the magento cache and log out of the administrator. Then log in as an administrator. In the store> Configuration, you can see the "Provider extension" tab. When you click on it, you can see the details of it.

+11
source

All Articles