skip to Main Content

I’m working on Magento 2.4 and I was creating a custom field in system.xml. I want to set the default value of this field as the URL of the store. How can do that?

Thank you

2

Answers


  1. Need to create config.xml in etc folder of module and we can configure default value as in below example

    <?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
        <storeCode>
            <sectionid>
                <groupid>
                    <fieldname></fieldname>
                </groupid>
            </sectionid>
        </storeCode>
    </config>
    

    to set the default value on store level need to change "storeCode" otherwise we can use "default"

    Login or Signup to reply.
  2. Yes, you can set the default value of custom field. For this create a below file in your module folder.

    etc/config.xml
    

    and add below code:

    <?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>
                     <sectionId>
                         <groupId>
                             <fieldId>default_value_store_url</fieldId>
                         </groupId>
                     </sectionId>
                 </default>
        </config>
    

    The sectionId/groupId/fieldId has to match the structure in the system.xml file.

    Clear cache and check the output.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search