How to Create System.xml Configuration in Magento 2

Steps To Create System.xml Configuration in Magento 2

Create System.xml

Directory

app/code/VendorName/ModuleName/etc/adminhtml/system.xml

Content for this file

<?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="tab1" translate="label" sortOrder="10">
            <label>Example Tab</label>
        </tab>
        <section id="exampletab2" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Example Tab2</label>
            <tab>tab1</tab>
            <resource>VendorName_ModuleName::exampletab2_config</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>

Set Default value

Directory

app/code/VendorName/ModuleName/etc/config.xml

Content for this file

<?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>
        <exampletab2>
            <general>
                <enable>1</enable>
                <display_text>Example Tab2</display_text>
            </general>
        </exampletab2>
    </default>
</config>

Flush Magento Cache

Please refresh your cache to check the result

Get value from the configuration

Simple calling

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

Create a helper file (standard)

app/code/VendorName/ModuleName/Helper/Data.php

<?php

namespace VendorName\ModuleName\Helper;

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

class Data extends AbstractHelper
{

	const XML_PATH_ModuleName = 'modulename/';

	public function getConfigValue($field, $storeId = null)
	{
		return $this->scopeConfig->getValue(
			$field, ScopeInterface::SCOPE_STORE, $storeId
		);
	}

	public function getGeneralConfig($code, $storeId = null)
	{

		return $this->getConfigValue(self::XML_PATH_ModuleName .'general/'. $code, $storeId);
	}

}

 

Now, we try in controller

app/code/VendorName/ModuleName/Controller/Index/Config.php

<?php

namespace VendorName\ModuleName\Controller\Index;

class Config extends \Magento\Framework\App\Action\Action
{

	protected $helperData;

	public function __construct(
		\Magento\Framework\App\Action\Context $context,
		\Mageplaza\HelloWorld\Helper\Data $helperData

	)
	{
		$this->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();

	}
}

 

Now, run command to clean cache:

php bin/magento cache:clean

 

If you are looking for Magento Developers, visit Magento Web Agency.

Related Products