![Magento 2 How to Create System.xml Configuration - Kishan Savaliya](https://www.kishansavaliya.com/media/magefan_blog/xMagento-2-How-to-Create-System-xml-Configuration-Kishan-Savaliya_1.png.pagespeed.ic.AVab6MpSs2.png)
Welcome to Kishan Savaliya's Magento 2 Blog.In this blog post, I will show you how to create system.xml Configuration. We can use system.xml file to create configuration fields in Magento 2 System Configuration.
The system.xml file is useful to create backend configuration, you will need this if you want some settings based on admin requirements. So admin user can easily set in Store > Settings > Configurations. and you can use it in your module.
To Create Backend system.xml Configuration :
- Create system.xml file.
- Create config.xml file to set default value.
- Clean and Flush Magento 2 cache.
- Get your configuration value.
Step : 1 - Create system.xml file.
Magento system configuration is devided into different sections like Tabs, Groups, Sections and Fields. For e.g.
In above image, you can better understand what is Tab, Section, Group and Fields in configuration. So let's start, how to create new system configuration in Magento 2.
Open you Magento 2 root directory and navigate inside your custom module, if you don't know how to create custom module in Magento 2 then this article can be useful to you. You can add your new system.xml file inside etc/adminhtml/ directory in Magento 2 module.
We will create system configuration when admin user can add their social profile links and we can use that in frontend.
app/code/SK/Configurations/etc/adminhtml/system.xml
Content for this file is..
<?xml version="1.0"?>
<!--
/**
* @package SK\Configuration
* @author Kishan Savaliya <kishansavaliyakb@gmail.com>
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
<system>
<tab id="sk_social_tab" translate="label" sortOrder="15">
<label><![CDATA[Social Profiles Tab]]></label>
</tab>
<section id="social_section" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Social Profiles Section</label>
<tab>sk_social_tab</tab>
<resource>SK_Configuration::config</resource>
<!-- Social Profiles -->
<group id="profile_group" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Social Profiles Group</label>
<field id="enable_field" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>social_section/profile_group/enable_field</comment>
</field>
<field id="facebook_field" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Facebook</label>
<comment>social_section/profile_group/facebook_field</comment>
</field>
<field id="instagram_field" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Instagram</label>
<comment>social_section/profile_group/instagram_field</comment>
</field>
<field id="twitter_field" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Twitter</label>
<comment>social_section/profile_group/twitter_field</comment>
</field>
</group>
<!-- Social Profiles -->
</section>
</system>
</config>
Here in above file, I have created "Social Profiles Tab" named tab, "Social Profiles Section" named section, "Social Profiles Group" named group and 4 different fields which is Enabled, Facebook, Instagram and Twitter links field.
Step : 2 - Create config.xml file to set default value.
If you want to set default system configuration value, then you can create config.xml file in etc/ directory in your module and you can define you default configuration value there. So let's create config.xml file
app/code/SK/Configurations/etc/config.xml
Content for this file is..
<?xml version="1.0"?>
<!--
/**
* @package SK\Configuration
* @author Kishan Savaliya <kishansavaliyakb@gmail.com>
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<social_section>
<profile_group>
<enable_field>1</enable_field>
<facebook_field>https://www.facebook.com/</facebook_field>
<instagram_field>https://www.instagram.com/</instagram_field>
<twitter_field>https://www.twitter.com/</twitter_field>
</profile_group>
</social_section>
</default>
</config>
So as per above file you can put your field's value inside <default></default> tag in config.xml file like..
<default>
<section_id>
<group_id>
<field_id>{default_value}</field_id>
</group_id>
</section_id>
</default>
Step : 3 - Clean and Flush Magento 2 cache.
Now just go to System > Cache management and clean and flush cache once, or you can do this using below command
php bin/magento c:c && php bin/magento c:f
after running above command, Go to your Store > Settings > Configuration. There you can see your new tab open that and see the result.
As you can see, we had defined global configuration here.
So if you want this configuration store wise or website wise, then you can set this in system.xml file's section, group and fields.. You can set 0 or 1 based on your requirements.
showInDefault="1" showInWebsite="1" showInStore="1"
Step :4 - Get your configuration value.
You can create one helper file in your module and you can create one global function that you can access everywhere.. So create helper file here.
app/code/SK/Configurations/Helper/Data.php
Content of this file is..
<?php
/*
* @package SK\Configuration
* @author Kishan Savaliya <kishansavaliyakb@gmail.com>
*/
namespace SK\Configuration\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
protected $scopeConfig;
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}
public function getConfig($configPath)
{
return $this->scopeConfig->getValue(
$configPath,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
Now we can use this function in phtml file easily by using below lines..
<?php $helper = $this->helper(\SK\Configuration\Helper\Data::class); ?>
<?php $isModuleEnabled = $helper->getConfig("social_section/profile_group/enable_field"); ?>
<?php $facebookUrl = $helper->getConfig("social_section/profile_group/facebook_field"); ?>
<?php $instagramUrl = $helper->getConfig("social_section/profile_group/instagram_field"); ?>
<?php $twitterUrl = $helper->getConfig("social_section/profile_group/twitter_field"); ?>
Now, you can use Helper in Controller, Block and any other files and you can get system configuration value.
Hope you may like this article and can understand this easily. You can add comments below in case if you have any questions regarding this article or if I missed anything here. I will check and get back to you with proper solution.
If you enjoyed this blog post, share it with friends!