In this blog post we will see how to setup System Configuration for your module in magento2
System configuration in magento2 is very similar to magento1, so you read basics of system configuration in magento here
System configuration is divided into different parts; tab, section, group and field which is described in magento1 blog. This same concepts are used in magento2 as well.
The system.xml file is located in adminhtml/ folder in magento2
Excellence/Hello/etc/adminhtml/system.xml
add the content is
<?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="helloworld" translate="label" sortOrder="100"> <!-- add a new tab with id helloworld --> <label>Hello World</label> </tab> <section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1"> <!-- add a new section with id helloworld and for tab helloworld --> <class>separator-top</class> <label>Hello World Configuration</label> <tab>helloworld</tab> <resource>Excellence_First::test_config</resource> <group id="active_display" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0"> <!-- add a new group with id active display --> <label>Hello World Configuration Options</label> <field id="scope" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0"> <!-- add a new field with id scope --> <label>Enable Helloworld Controller</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> </group> </section> </system> </config>
Above we can see how to add a new tab, section, group and field
Next to set default values for these configuration item, create the file
Excellence/Hello/etc/config.xml
and add the code
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <helloworld> <active_display> <scope>1</scope> </active_display> </helloworld> </default> </config>
There is another important thing to do, in our system.xml we had the line
Excellence_First::test_config
This is used to setup ACL for system configration. How this works in detail we will see later, but for now create the file
Excellence/Hello/etc/acl.xml
with the code
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd"> <acl> <resources> <resource id="Magento_Adminhtml::admin"> <resource id="Magento_Adminhtml::stores"> <resource id="Magento_Adminhtml::stores_settings"> <resource id="Magento_Adminhtml::config"> <resource id="Excellence_First::test_config" title="Hello World Section" /> </resource> </resource> </resource> </resource> </resources> </acl> </config>
Now when you open magento admin, you see the HelloWorld tab and sections.
You might get an error 404 Not Found first time, just logout and login again to fix this
If you want to explore further system configuration options which magento2 provides, look at the module Magento/Config/Model/Config files. Here you can see other system configuration options which magento provides. You can also explore other system.xml files from core magento modules .
The post Magento2 – system.xml appeared first on Excellence Technologies Magento Blog | Magento Tutorials | Magento Developer.