Magento 2 create system configuration


Magento 2 create system configuration (system.xml)– It is very simple to create system configuration in Magento 2, it is a little bit different than the magento 1. In magento 2 we need to create some additional files to create system configuration. Here in this tutorial, we are going to explain how you can create system configuration in Magento 2. We are going to create a simple HelloWorld module to explain this functionality, you can directly download this module and use it.


Magento 2 create system configuration Settings Example | Custom System.xml

You need to create the following two files to create system config settings-

  • 1). app/code/Tutorialsplane/HelloWorld/etc/adminhtml/system.xml
  • 2). app/code/Tutorialsplane/HelloWorld/etc/acl.xml
  • 3). app/code/Tutorialsplane/HelloWorld/etc/config.xml

Now let us go one by one to create the above files-

1. Create System.xml

First create system.xml file in app/code/Tutorialsplane/HelloWorld/etc/adminhtml/system.xml and then add the following code to create tabs, sections, group and fields-

Magento 2 create system configuration Example:

<?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" sortOrder="999" translate="label">
			<label>helloworld</label>
		</tab>
		<section id="basic_settings" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
			<label>basic_settings</label>
			<tab>helloworld</tab>
			<resource>Tutorialsplane_HelloWorld::config_tutorialsplane_helloworld</resource>
			<group id="check" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
				<label>check</label>
				<field id="enabled" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="select">
					<label>enabled</label>
					<comment/>
					<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
				</field>
			</group>
		</section>
	</system>
</config>

In the above example we have created a simple setting having option – yes/no. On the same way you can create your own settings.

2. Create acl.xml

Now create acl.xml and add the following code –

System Setting acl.xml Example:

<?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_Backend::admin">
				<resource id="Magento_Backend::stores">
					<resource id="Magento_Backend::stores_settings">
						<resource id="Magento_Config::config">
							<resource id="Tutorialsplane_HelloWorld::config_tutorialsplane_helloworld" title="basic_settings"/>
						</resource>
					</resource>
				</resource>
			</resource>
		</resources>
	</acl>
</config>

3. Add default value in config.xml

Now add the default system configuration value in config.xml.

Add default value in config.xml Example:

<?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>
		<basic_settings>
			<check>
				<enabled/>
			</check>
		</basic_settings>
	</default>
</config>

In the above example we have created a simple setting for HelloWorld Module, on the same way you can create multiple settings for you module.

Download Sample Module

You can download the sample module simply by clicking this – Tutorialsplane_HelloWorld


Advertisements

Add Comment

📖 Read More