{"id":11955,"date":"2021-03-30T09:00:32","date_gmt":"2021-03-30T03:30:32","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11955"},"modified":"2021-03-30T09:25:53","modified_gmt":"2021-03-30T03:55:53","slug":"set-default-values-system-configuration-fields","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/set-default-values-system-configuration-fields\/","title":{"rendered":"Magento 2 &#8211; How to Set Default Values for System Configuration Fields"},"content":{"rendered":"<p>In this post I am going to explain how to Set Default Values for System Configuration Fields in Magento 2.<\/p>\n<p>First we will make one &#8220;system.xml&#8221; file at below path.<\/p>\n<p><strong>Path: app\/code\/&lt;vendor&gt;\/&lt;module&gt;\/etc\/adminhtml\/system.xml<\/strong><\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\" ?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\"&gt;\r\n    &lt;system&gt;\r\n        &lt;tab id=\"ht\" translate=\"label\" sortOrder=\"999999\"&gt;\r\n            &lt;label&gt;HT&lt;\/label&gt;\r\n        &lt;\/tab&gt;\r\n        &lt;section id=\"mymodulesection\" translate=\"label\" type=\"text\" sortOrder=\"30\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n            &lt;class&gt;separator-top&lt;\/class&gt;\r\n            &lt;label&gt;My Module&lt;\/label&gt;\r\n            &lt;tab&gt;ht&lt;\/tab&gt;\r\n            &lt;resource&gt;Magento_Config::config_admin&lt;\/resource&gt;\r\n            &lt;group id=\"general\" translate=\"label\" type=\"text\" sortOrder=\"10\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n                &lt;label&gt;General&lt;\/label&gt;\r\n                &lt;field id=\"enabled\" translate=\"label\" type=\"select\" sortOrder=\"5\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n                    &lt;label&gt;Enabled&lt;\/label&gt;\r\n                    &lt;source_model&gt;Magento\\Config\\Model\\Config\\Source\\Yesno&lt;\/source_model&gt;\r\n                &lt;\/field&gt;\r\n                &lt;field id=\"welcome_text\" translate=\"label\" type=\"text\" sortOrder=\"10\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n                    &lt;label&gt;Welcome Text&lt;\/label&gt;\r\n                &lt;\/field&gt;\r\n            &lt;\/group&gt;\r\n        &lt;\/section&gt;\r\n    &lt;\/system&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>Now, we need to set default values for above system config fields.<\/p>\n<p>For that add one file &#8220;config.xml&#8221; at below path.<\/p>\n<p><strong>Path: app\/code\/&lt;vendor&gt;\/&lt;module&gt;\/etc\/config.xml<\/strong><\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/Store\/etc\/config.xsd\"&gt;\r\n    &lt;default&gt;\r\n        &lt;mymodulesection&gt;\r\n            &lt;general&gt;\r\n                &lt;enabled&gt;1&lt;\/enabled&gt;\r\n                &lt;welcome_text&gt;Welcome&lt;\/welcome_text&gt;\r\n            &lt;\/general&gt;\r\n        &lt;\/mymodulesection&gt;\r\n    &lt;\/default&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>The path of the field is &#8220;section\/grounp\/field&#8221;.<\/p>\n<p>Here our section is &#8220;mymodulesection&#8221;, group is &#8220;general&#8221; and fields are &#8220;enabled&#8221; and &#8220;welcome_text&#8221;.<\/p>\n<p>This code will make default system config value when you install module.<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1775803169\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to Set Default Values for System Configuration Fields in Magento 2. First we will make one &#8220;system.xml&#8221; file at below path. Path: app\/code\/&lt;vendor&gt;\/&lt;module&gt;\/etc\/adminhtml\/system.xml &lt;?xml version=&#8221;1.0&#8243; ?&gt; &lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:module:Magento_Config:etc\/system_file.xsd&#8221;&gt; &lt;system&gt; &lt;tab id=&#8221;ht&#8221; translate=&#8221;label&#8221; sortOrder=&#8221;999999&#8243;&gt; &lt;label&gt;HT&lt;\/label&gt; &lt;\/tab&gt; &lt;section id=&#8221;mymodulesection&#8221; translate=&#8221;label&#8221; type=&#8221;text&#8221; sortOrder=&#8221;30&#8243; showInDefault=&#8221;1&#8243; showInWebsite=&#8221;1&#8243; showInStore=&#8221;1&#8243;&gt; &lt;class&gt;separator-top&lt;\/class&gt; &lt;label&gt;My&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/set-default-values-system-configuration-fields\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":12276,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[729,9,10,2153],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11955"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11955"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11955\/revisions"}],"predecessor-version":[{"id":13179,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11955\/revisions\/13179"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/12276"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}