Categories: How ToMagento 2

How to Create System.xml Configuration in Magento 2

At the time of creating new extension in Magento 2, you need to provide admin user with some settings prior to make it work. System.xml is a configuration file which is used to create those setting fields in extension and it is placed under “etc” folder. After the successful creation of settings, these fields are shown under Store –> Configuration –> Your Extension.

Here, we will learn to create system.xml configuration in Magento 2.

Go to app/code/Magecomp/Hello/etc/adminhtml/system.xml and add the code below:

<pre class="lang:default decode:true">
<?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>Magecomp_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>
</pre>

With above code, we can add new tab, section, group and field. Now the next step is to set default values for these configuration item, create the file app/code/Magecomp/Hello/etc/config.xml and add the code below:

<pre class="lang:default decode:true">
<?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>
</pre>

There is another important thing to do is to setup ACL. In our system.xml, we have the line
Magecomp_First::test_config
This is used to setup ACL for system configuration. Create a file app/code/Magecomp/Hello/etc/acl.xml with below code:

<pre class="lang:default decode:true">
<?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="Magecomp_First::test_config" title="Hello World Section" />
                        </resource>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>
</pre>

Now open Magento admin and you can see the HelloWorld tab and sections.

Hope this tutorial has helped you to create system.xml configuration for your next Magento 2 extension.

Let me know if you stuck somewhere or require any help regarding through commenting. I will be happy to help you.

Click to rate this post!
[Total: 9 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago