Let me tell you scenario where you are delivering a Magento 2 custom developed module to a store merchant who is a Magento 2 newbie. For the extension to work as expected, certain configurations are meant to have particular values. But the store merchant or admin may not be aware of it or may not have an idea of which value should be added to get the desired output. To ease the configuration and avoid empty value output, you can set default values for Magento 2 system configuration.
By setting default values for Magento 2 system configuration, you ensure that there will always be a logical value returned for the configuration option and not have to worry about an invalid or empty output. As soon as the extension is installed, the Magento 2 system configuration is set to the default values. It makes the task of admin/module user easier.
The post shows the method to set default values for Magento 2 system configuration.
Method to Set Default Values for Magento 2 System Configuration:
- Your system.xml file will be something like this:
<?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="meetanshi" translate="label" sortOrder="50"> <label><![CDATA[<img src="https://meetanshi.com/media/logo.png" alt="Meetanshi" height="20" style="vertical-align:middle;"/>]]></label> </tab> <section id="extension" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1"> <class>separator-top</class> <label>Extension</label> <tab>meetanshi</tab> <resource>Vendor_Extension::config_extension</resource> <group id="general" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Configuration</label> <field id="enable" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Extension</label> <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model> </field> </group> </section> </system> </config>
- Create Config.xml at Vendor\Extension\etc\ directory
<?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> <extension> <general> <enable>1</enable> </general> </extension> </default> </config>
Follow the above method and leave no chance to entertain the newbies who use your extension with complaints due to their lack of knowledge in extension configuration!
Thank you.