Magento 2 System.xml Configuration – Default Values Setup

configurationmagento2modulesystem.xml

In Magento 1, it was possible to

  1. Configuration a user interface for the System Configuration section in etc/system.xml files

  2. Set default values for those fields in the etc/config.xml files

Behind the scenes Magento would load data from core_config_data, and if nothing was set, would default to the values set globally in etc/config.xml. (simplified version — it's a little more complicated than that)

Can the same thing be done in Magento 2? I know it's possible to configure UI elements via system.xml — but is it possible to set default values for these settings? If so, where or how should these values be configured?

Best Answer

yes, Magento 2 still allows you to define the default values inside a configuration file. namespace\modulename\etc\config.xml

<?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>
            <sectionname>
                <groupname>
                    <fieldid>1</fieldid>
                </groupname>
            </sectionname>
        </default>
    </config>

System configuration system.xml

<?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="namespace_tab" translate="label" sortOrder="1">
                <label>COnfig Title</label>
            </tab>
            <section id="sectionname" translate="label" sortOrder="1" showInDefault="1" 
    showInWebsite="1" showInStore="1">
                <label>Some Title</label>
                <tab>namespace_tab</tab>
                <resource>Namespace_Modulename::system_config</resource>
                <group id="groupname" translate="label" type="text" sortOrder="1" showInDefault="1" 
    showInWebsite="1" showInStore="1">
                    <label>Some Configuration</label>
                    <field id="fieldid" translate="label" type="select" sortOrder="1" 
    showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Enable in frontend</label>
                        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    </field>
                 </group>   
            </section>
        </system>
    </config>
Related Topic