Skip to main content

Magento 2 - Add to System Configuration


In magento 2, the system configuration has gone little changes. Now we can access the configuration from Stores > Settings > Configuration. Also, there is small changes in the system.xml file also. Now the system.xml file is inside etc/adminhtml/ folder. You can find the sample code in the git repo.

Add the files as per the blog post. The system.xml file have section, group and field tag and the details has to be entered in the tag instead of sub tabs. If we are creating new section, then have to mention the resource by which we can assign permission to the users.
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="vendorname_samplemodule" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Sample Module</label>
            <tab>catalog</tab>
            <resource>VendorName_SampleModule::config</resource>
            <group id="settings" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Settings</label>
                <field id="active" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="my_message" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>My Message</label>
                </field>
            </group>
        </section>
    </system>
</config>


For the above code, we need to assign the permission to view and edit the configuration. This is new to magneto 2, as we had only one permission to edit the configuration earlier in magento 1. The acl.xml will look as below.
<?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="VendorName_SampleModule::config" title="Sample Module" />
                        </resource>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>


Comments

Popular posts from this blog

Magento 2 - Create Attributes in Setup

Sometimes in our module we require to create product or category attributes automatically on module install. Also, we might require to add extra fields to quote, sales order, invoice, creditmemo tables. We will go through how to do that in magento 2. You can find the sample code in the git repo .