Pages

Thursday, September 10, 2015

Magento 2: Create configuration file system.xml in our custom module


Yes, In magento 2 we can create system configuration file same as Magento 1. But we need to create some file. Lets start how to create it

We need to use following file to create it.

app/code/Stepbystepprograming/Helloworld/etc/adminhtml/system.xml

app/code/Stepbystepprograming/Helloworld/etc/acl.xml

This 2 files are important to create system configuration.

In System.xml file

Adding the common content

<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
<system>
.......
</system>
</config>



 Now we need to add the Tab
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
<system>
<!-- Add new Tab -->
<tab id="Stepbystepprograming" translate="label" sortOrder="300">
<label>Stepbystepprograming Extension</label>
</tab>
</system>
</config>



 Add new Section in the file
<section id="helloworld" translate="label" type="text" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Helloworld</label>
<tab>Stepbystepprograming</tab>
<!-- resource tag name which we have to defined in the acl.xml -->
<resource>Stepbystepprograming_Helloworld::config_helloworld</resource>
</section>



 Create new group and field on section in file
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Options</label>
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>



 So now System.xml file should display like:
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
<system>
<!-- Add new Tab -->
<tab id="Stepbystepprograming" translate="label" sortOrder="300">
<label>Stepbystepprograming Extension</label>
</tab>
<section id="helloworld" translate="label" type="text" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Helloworld</label>
<tab>Stepbystepprograming</tab>
<!-- resource tag name which we have to defined in the acl.xml -->
<resource>Stepbystepprograming_Helloworld::config_helloworld</resource>
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Options</label>
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>


Now we need to create acl.xml file

In the file we need to write the below content

<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/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">
<!-- this resource id we can use in system.xml for section -->
<resource id="Stepbystepprograming_Helloworld::config_helloworld" title="Helloworld Section" sortOrder="80" />
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>


After that, Clear the magento cache & logout from admin side. Then Login at admin side. In store >> Configuration you can see the tab “Stepbystepprograming Extension”. When you click on this you can see the detail of this.

We hope, It will helpful to you.

No comments:

Post a Comment