24

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?

  • I am developing an extension where in the ADMIN store>>configuration my extension setting i want to put attribute draggable and sortable list and therefore i need to set custom template for this particular field, so is there any way i can put custom template phtml file inside system.xml? – Yogesh Trivedi Oct 14 '16 at 3:57
42

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>
  • 1
    Note that sectionname, groupname, and fieldid all correlate to the tag id of each section. – Eirik Nov 19 '15 at 23:15
  • how can we get this default value in .php file – Anand Ontigeri Apr 27 '16 at 12:31
  • 1
    @AnandOntigeri use $this->scopeConfig->getValue( $path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); Where $this->scopeConfig must be instantiated in class __construct() method. \Magento\Framework\Cache\ConfigInterface $scopeConfig – Vasilii Burlacu Dec 23 '16 at 7:34
  • Is it possible to get the default values instead of the configured values? Vasilii's comment gives the current value, but I want to compare the current configuration of a site to the default values (and if possible, reset it). Can that be done without loading and reading the config.xml manually ? – Jacques May 10 '17 at 14:38
  • @JaccoAmersfoort The default values are only accessible until they are overwritten. Magento 2 stores the system values in the core_config_data table and does not store the default values separate from the current value. – Eirik May 11 '17 at 0:48
7

Magento2 allow you to set default value same like Magento1. When you look at the contact module from Magento2,

system.xml for contact module

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="contact" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Contacts</label>
            <tab>general</tab>
            <resource>Magento_Contact::contact</resource>
            <group id="contact" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Contact Us</label>
                <field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable Contact Us</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <backend_model>Magento\Contact\Model\System\Config\Backend\Links</backend_model>
                </field>
            </group>
            <group id="email" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Email Options</label>
                <field id="recipient_email" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Send Emails To</label>
                    <validate>validate-email</validate>
                </field>
                <field id="sender_email_identity" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Email Sender</label>
                    <source_model>Magento\Config\Model\Config\Source\Email\Identity</source_model>
                </field>
                <field id="email_template" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Email Template</label>
                    <comment>Email template chosen based on theme fallback when "Default" option is selected.</comment>
                    <source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

and config.xml for the contact module

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
            <contact>
                <enabled>1</enabled>
            </contact>
            <email>
                <recipient_email>
                    <![CDATA[hello@example.com]]>
                </recipient_email>
                <sender_email_identity>custom2</sender_email_identity>
                <email_template>contact_email_email_template</email_template>
            </email>
    </default>
</config>

If we want to set default value, then we have to match its id like

<section id="contact"> <group id="contact"> <field id="enabled">

then it becomes

 <default>
     <contact>
         <enabled>1</enabled>
     </contact>
 </default>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.