System configuration in admin Magento2

System configuration mainly used as module configuration where the admin will set some configuration for specific modules like enable/disable the module. Get some input or selection of options like that we have a variety of field to be created in the configuration.

Where you will find it?
You will be able to find the module configuration in Magento 2 admin -> Stores(Menu) -> Configuration

All the system configuration will have three parts:

  1. Tab (It is left side the system configuration page. Which contains multiple sections of multiple modules)
  2. The section will contain the groups of configuration for the specific module
  3. Each group will contain fields for the configuration

Few basic attributes are used in all the elements of system configuration are:

  1. Id: unique ID of the element
  2. Translate: To tell Magento to translate the label of the element according to the language of the admin
  3. sortOrder: Position of the element.
  4. showInDefault: Do you want to show this element when an admin hasn’t selected any scope. (Global)
  5. showInWebsite: Do you want to show this element when admin select website (Website)
  6. showInStore: Do you want to show this element when admin select store view (Store View)

Above mentioned 3,4,5 point mainly useful when we want to allow admin customer to configure the module based on the scope.

SCOPE in configuration.

Store: It is configuration limited to the store view, which is often used to display
a store in multiple languages.
Website: Configuration is limited to the website.
Global: It applies to the entire installation.

For example enable/disable of module if we can showInDefault=1 , ShowIndWebsite=1 and ShowInStore=1 then will show this field in all scope. If make Show InDefault=0, ShowInWebsite and ShowInStore=1 then it will show field in default or global scope admin have to change the scope.

<system> 
     <tab id="codedecorator" translate="label" sortOrder="10">
       <label>Codedecorator</label> <!--Tab Name-->
     </tab>
     <section id="one" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
       <class>one-section</class> <!--  Class name to section -->
       <label>Hello World</label> <!--Label Of this section -->
       <tab>codedecorator</tab> <!-- It will tell which tab is linked with this section so it should match with tab id and also can be used to group multiple section -->
       <resource>Codedecorator_Learn::codedecorator_config</resource> <!--  It used in ACL -->
       <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>License Configuration</label>
                <!-- Field element has type attribute which decide the type of field. -->
                <field id="serial_key" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Serial Key</label>
                    <backend_model>Codedecorator\Learn\Model\Config\Backend\Lookup</backend_model>
                    <validate>required-entry</validate>
                    <comment>Take serial key from Codedecorator</comment> <!--Give a comment to field -->
                </field>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
       </group>
    </section>
</system>

Source Model: Source is model to provide data to field like select and multiple select.
Backend Model: Backend model is mainly useful when want to take some action before saving the configuration. Like to check serial key correct or not before saving the configuration if wrong provides a message that is not correct.

Happy coding guys!

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

Leave a Reply

Your email address will not be published.