meta charset='UTF-8'/> Magento 2 How to Create System.xml Configuration | Magento 2 Store
Rated 5/5 based on 100 votes

Magento 2 How to Create System.xml Configuration

magento 2 system configuration

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.

To Create system.xml

  • Step 1: Create System.xml
  • Step 2: Set default value
  • Step 3: Flush Magento cache

Step 1: Create System.xml

The magento 2 system configuration page is divided logically in few parts: Tabs, Sections, Groups, Fields. Please check this images to understand about this:
magento 2 system configuration
So let’s start to create a simple configuration for the simple Module Hello World. The system.xml is located in etc/adminhtml folder of the module, we will create it a new Tab for our vendor “Mageplaza”, a new Section for our module Hello World, a Group to contain some simple fields: enable module and text.
File: app/code/Mageplaza/HelloWorld/etc/adminhtml/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="mageplaza" translate="label" sortOrder="10">
            <label>Mageplaza</label>
        </tab>
        <section id="hello" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Hello World</label>
            <tab>mageplaza</tab>
            <resource>Mageplaza_HelloWorld::hello_configuration</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Display Text</label>
                    <comment>This text will display on the frontend.</comment>
                </field>
            </group>
        </section>
    </system>
</config>
Checking this code, you will see how to create a Tab, Section, Group and Field. We will find more detail about each element:
  • The Tab element may have many sections and some main attributes and child:
    • Id attribute is the identify for this tab
    • sortOrder attribute will define the position of this tab.
    • Translate attribute let Magento know which title need to translate
    • Label element child is the text which will show as tab title.
  • The Section element will have id, sortOrder, translate attributes like the Tab element. Some other attributes (showInDefault, showInWebsite, showInStore) will decide this element will be show on each scope or not. You can change the scope here
magento 2 system configuration
The section may have many group and some other child elements:
  • Class: this value will be added as class for this element. You should you it if you want to make-up this element.
  • Label: the text title of this element
  • Tab: this’s a tab id. This tab element will let Magento know the tab which this section is belong to. This section will be placed under that tab
  • Resource: defined the ACL rule which the admin user must have in order to access this configuration.
  • Group: This element may have many field and some attributes which is same as Sections.
  • Fields: is the main path of this page. It will save the data which we want to setting. In this element, we focus on the type attribute. It will define how the element is when display. It can be: text, select, file… In this example we create 2 fields with type select and text. With each type we will define the child element for the field to make it work as we want.
For example, with the type select/multiselect you must define the child element resource_model.

Step 2: Set default value

Each field in system.xml after create will not have any value. When you call them, you will receive ‘null’ result. So for the module, we will need to set the default value for the field and you will call the value without go to config, set value and save it. This default value will be saved in config.xml which is located in etc folder. Let’s create it for this simple configuration:
File: app/code/Mageplaza/HelloWorld/etc/config.xml
<?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>
        <hello>
            <general>
                <enable>1</enable>
                <display_text>Hello World</display_text>
            </general>
        </hello>
    </default>
</config>
You can put the path to the field in the <default> element to set value default for it. The format is:
<default>
    <section>
        <group>
            <field>{value}</field>
        </group>
    </section>
</default>

Step 3: Flush Magento Cache

Now, please refresh your cache and see the result:
general configuration admin
Note that, if you might get an Error 404 Page Not Found first time, just logout and login again to fix this.

Share on Google Plus

About Hoàng Kang

Name: Nguyễn Tuấn Hoàng

 photo 1f451_zpsp5spiubu.png If you're good at something, never do it for free  photo 1f451_zpsp5spiubu.png

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment