Reading list Switch to dark mode

    How to set default values for configuration fields in Magento 2

    Updated 18 December 2017

    Today we will learn how we can set the default values for our module’s configuration fields.
    I am assuming that you have a good understanding Magento 2 directory structure .
    So I will not be covering that you can read our Guide for this Magento 2 basic module.
    I hope you must have read the above Guide.
    Now let’s Start ,
    Step 1 : create file in this location for creating configuration fields –
    app/code/<vendor_name>/<module_name>/etc/adminhtml/system.xml

    <?xml version="1.0"?>
    <!-- /**
    * Webkul Software.
    *
    * @category Webkul
    * @package Webkul_Wkmodule
    * @author Webkul
    * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
    * @license https://store.webkul.com/license.html
    */ -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
        <system>
          <section id="payment">
              <group id="wkmodule" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                  <label><![CDATA[Test Payment Module]]></label>                  
                  <field id="active" translate="label" type="select" sortOrder="101" showInDefault="1" showInWebsite="1" showInStore="1">
                      <label>Enabled</label>
                      <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                  </field>
              </group>
          </section>
      </system>
    </config>
    
    

    Step 2 : Create file to set default values here :
    app/code/<vendor_name>/<module_name>/etc/config.xml

    <?xml version="1.0"?>
    <!--
    /**
    * Webkul Software.
    *
    * @category  Webkul
    * @package   Webkul_Wkmodule
    * @author    Webkul
    * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
    * @license   https://store.webkul.com/license.html
    */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
        <default>
            <payment>
                <wkmodule>
                    <active>0</active>
                    <------>x</---->
                </wkmodule>
            </payment>
        </default>
    </config>

    Similarly, you can set the default values for the other fields as well.

    Hope this blog may help.
    If you want to add button in configuration so please follow this wonder full blog :
    add Button in magento 2 configuration section .
    If you have any Query then feel free to ask in below comment section.

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home