create-configuration-magento-2

How To Create system.xml Configuration In Magento 2

Create Custom configuration options in Magento 2 can provide additional functionality and customization options for your module or theme. The system.xml file is the primary file used to define these configuration settings, and it allows developers to easily create fields such as text fields, dropdowns, checkboxes etc. In this guide, we will walk through the steps to create a system.xml configuration in Magento 2.

Steps to create system.xml in Magento 2

  • Create module
  • Create system.xml
  • Clear cache
  • Create a helper & Get configuration Values

Create Module In Magento 2

In the previous article, we discussed in detail to create a custom module in Magento 2.

Create the system.xml file In Magento 2

The system.xml file is an important configuration file in Magento 2 that allows developers to add new configuration options to the admin panel. It is used to create custom configuration settings for a module or theme, which can then be accessed and modified by admin users.

Directory

app/code/GDBlogger/DemoModule/etc/adminhtml/system.xml

Code of 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="gdblogger" translate="label" sortOrder="100">
            <label>GD Blogger</label>
        </tab>
        <section id="settings" translate="label" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Hello World</label>
            <tab>gdblogger</tab>
            <resource>GDBlogger_DemoModule::config_settings</resource>
            <group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Settings</label>
                <field id="enable" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="text" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Text</label>
                </field>
            </group>
        </section>
    </system>
</config>
Output:

This is an example of a system.xml configuration file in Magento 2. Let’s go through each of the XML tags and what they do:

config:

This is the root tag of the XML file and defines the configuration settings for the module or theme.

system:

This tag contains all the system configuration settings for the module or theme.

tab:

This tag defines a new tab in the admin panel for the configuration settings. In this example, a new tab is defined with the ID gdblogger and the label GD Blogger.

section:

This tag defines a new section within the tab, which contains the actual configuration settings. The id attribute sets the ID of the section, and the translate attribute specifies whether the label should be translated. The showInDefault, showInWebsite, and showInStore attributes determine whether the section should be displayed in the respective scope.

label:

This tag specifies the label for the tab or section.

tab:

This tag indicates the tab where the section should appear.

resource:

This tag specifies the ACL resource ID that controls access to the configuration settings.

group:

This tag defines a new group within the section, which contains related configuration settings. The id attribute establishes the group’s ID, and the translate attribute determines whether to translate the label. The showInDefault, showInWebsite, and showInStore attributes indicate whether to display the group in the corresponding scope.

field:

This tag defines a new configuration setting within the group. The id attribute identifies the setting’s ID, and the translate attribute indicates whether to translate the label. The type attribute specifies the type of field (in this example, select and text). The sortOrder, showInDefault, showInWebsite, and showInStore attributes determine the order and scope of the field. The label tag specifies the label for the field. The source_model tag specifies the source model for the select field.

Clear Cache In Magento 2

After creating the system.xml, clear the cache to check the configuration in the admin panel. Run the following command to clear Magento 2 cache.

Php bin/magento cache:clean

Create Helper & Get Config Values

To get system configuration values, you need to create a helper in the module directory. If you don’t know how to create a helper in Magento 2. In the previous article, we discussed in detail to create a helper in Magento 2.

Directory

app/code/GDBlogger/DemoModule/Helper/Data.php

Code of Data.php

<?php

namespace GDBlogger\DemoModule\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
	Protected $scopeConfig;

	Public __construct(
	ScopeInterface $scopeConfig
        ){
	$this->scopeConfig = $scopeConfig;
        }

	public function getEnableConfig($code, $storeId = null)
	{
             return $this->scopeConfig->getValue(‘settings/general/enable’, $storeId);
	}

	public function getTextConfig($code, $storeId = null)
	{
             return $this->scopeConfig->getValue(‘settings/general/text’, $storeId);
	}
}

You can these helper functions in your controller and phtml file to show data on the front end.

I hope this guide will be helpful to you. Please do not hesitate to contact us through the comments section if you have any further questions.

Leave a comment

Your email address will not be published. Required fields are marked *