How to Set Default Values for Magento 2 System Configuration
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:
12345678910111213141516171819202122232425<?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
12345678910<?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!
Please use the Comments section below to mention your doubts about the topic. I’d be happy to help.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
1 Comment
Simple and Easy to follow. Thanks for sharing this tutorial.