Magento 2: Default Values for `system.xml` Configuration
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>
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[[email protected]]]>
</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>
<contact>
<enabled>1</enabled>
</contact>
</contact>
</default>