The system.xml file is a configuration file used to create configuration fields in Magento 2 System Configuration. You will need this file if your module has settings that the admin needs to configure. You can go to Store -> Settings -> Configuration to see how it looks.

For Create system.xml

  • Step 1: Create system.xml
  • Step 2: Set default value
  • Step 3: Flush Magento cache
  • Step 4: Get value from configuration

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:

Magento2

Let's start by creating a simple configuration for the Hello World module. The system.xml file is located in the etc/adminhtml folder of the module. We will create a new tab for our vendor “Mavenbird,” a new section for our Hello World module, and a group to contain some simple fields: enable module and text.

File: app/code/Mavenbird/HelloWorld/etc/adminhtml/system.xml



    
        
            
        
        
separator-top mavenbird Mavenbird_HelloWorld::helloworld_config Magento\Config\Model\Config\Source\Yesno This text will display on the frontend.

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 can contain multiple sections and has several main attributes and children:
    • Id attribute: Identifies the tab.
    • sortOrder attribute: Defines the position of the tab.
    • translate attribute: Indicates which title should be translated.
    • Label child: Specifies the text that will appear as the tab title.
  • The Section element also includes id, sortOrder, and translate attributes, similar to the Tab element. Additional attributes such as showInDefault, showInWebsite, and showInStore determine whether this element will be displayed in each scope, allowing you to adjust the visibility accordingly.
Magento2

The section may have many group and some other child elements:

  • Class: This value will be added as a class for the element. Use it if you want to style this element.
  • Label: The text title associated with this element.
  • Tab: This is the tab ID. It indicates to Magento which tab this section belongs to, placing the section under that tab.
  • Resource: Defines the ACL rule that the admin user must have to access this configuration.
  • Group: This element can contain multiple fields and has attributes similar to those in Sections.
  • Fields: This is the primary path for this page, where data settings are saved. Focus on the type attribute, which determines how the element is displayed. Possible types include: text, select, file, etc. In this example, we create two fields with types select and text. For each type, we define child elements to customize functionality.

For example, with the type select/multiselect you must define the child element source_model.

Step 2: Set default value

Each field in system.xml will initially have no value, resulting in a ‘null’ response when called. To address this for the module, we need to set default values for the fields so that you can access them without having to navigate to the configuration, set values, and save them. These default values will be saved in config.xml, which is located in the etc folder. Let's create it for this simple configuration:

File:app/code/Mavenbird/HelloWorld/etc/config.xml



    
        
            
                1
                Hello World
            
        
    

You can put the path to the field in the element to set value default for it. The format is:


    
{value}

Step 3: Flush Magento Cache

Step 4: Get value from configuration

First, let's save the values and flush the cache. After that, you can retrieve the saved values from the database.

In system.xml, we have added two fields: enable and display_text. Therefore, the path should be:

  • helloworld/general/enable
  • helloworld/general/display_text

4.1 Simple calling:

$this->scopeConfig->getValue('helloworld/general/enable', 
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$this->scopeConfig->getValue('helloworld/general/display_text', 
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);

4.2 Create a helper file (standard)

Create file: app/code/Mavenbird/HelloWorld/Helper/Data.php

scopeConfig->getValue(
			$field, ScopeInterface::SCOPE_STORE, $storeId
		);
	}

	public function getGeneralConfig($code, $storeId = null)
	{
		return $this->getConfigValue(self::XML_PATH_HELLOWORLD .'general/'. $code, $storeId);
	}

}

Now, let's try to retrieve the values in the controller.

File: app/code/Mavenbird/HelloWorld/Controller/Index/Config.php

helperData = $helperData;
		return parent::__construct($context);
	}

	public function execute()
	{
		// TODO: Implement execute() method.

		echo $this->helperData->getGeneralConfig('enable');
		echo $this->helperData->getGeneralConfig('display_text');
		exit();
	}
}

Please run php bin/magento cache:clean to clear cache and check the result.