1

How to add default values for system configuration in magento 2.

I need to create and assign default values for system configuration(system.xml) for my custom module.

I have already created system configuations for my custom module.

In ui-form I have a field(flieuploader) namely small image which contains **backend system configuration which I have shown in below image**.

For below system configuartion enter image description here

I have reffered to set system configuation like below using \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray.php and \Magento\Config\view\adminhtml\templates\page\system\config\form\field\array.phtml

Now,It works good and the configuation is also saved in DB. But I need **to create default values for my system configuration in config.xml. Please provide me a solution.

In core_config table My system config value is stored in below format for small image

{"_1517563385604_604":{"width":"480","height":"300","imagesize":"25900"}}

For imagefile type

jpg,jpeg,png,gif

How Do I give this value in config.xml which is in default 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>
        <section id="aaa" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>XXXXX</label>
            <tab>x_y_tab</tab>
            <resource>X_Y::config</resource>
            <group id="groupslide" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>YYYYY</label>
                <field id="small_image" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                    <label>Small Image</label>
                    <frontend_model>X\Y\Block\Adminhtml\System\Config</frontend_model>
                    <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>                    

                </field>

                <field id="image_file_types" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                    <label>Image File Types</label>
                    <comment>Multiple values are seperated with comma</comment>
                </field>
            </group>
        </section>
    </system>
</config>
14
  • Do you want to create a new system configuration value setting ?
    – MGento
    Commented Feb 5, 2018 at 10:08
  • @MGento, I have updated my question Please answer
    – Jaisa
    Commented Feb 5, 2018 at 10:10
  • I think you want to create default configurations for your image dimensions in Store=>Configuration=> Catalog . Correct me if I am wrong
    – MGento
    Commented Feb 5, 2018 at 10:14
  • 1
    If user failed to enter dimensions means , Default dimensions will work.
    – Jaisa
    Commented Feb 5, 2018 at 10:22

3 Answers 3

1

You'll need to create config values in config.xml, you can take reference about this from any module which add entries in configuration and to use it in your phtml, check for value in db and if value is not available or null, set the value from core/config.

Let me know if you need code reference too to get this sorted.

1
  • See the updated post ans answer.Please correct me if i am wrong
    – Jaisa
    Commented Feb 7, 2018 at 8:45
2

Default value does not work with ArraySerialized in Magento 2.0.x – 2.1.x. Check this issue on github for details. To resolve it you can use data installation scripts in Setup/InstallData.php. Sample function could look like this

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $index = time();
        $configData = [
            'section' => 'nwdarrayserialized',
            'website' => null,
            'store'   => null,
            'groups'  => [
                'general' => [
                    'fields' => [
                        'responsiveItems' => [
                            'value' => array(
                                $index.'_0' => array("breakpoint" => "0", "items" => "1"),
                                $index.'_1' => array("breakpoint" => "480", "items" => "2"),
                                $index.'_2' => array("breakpoint" => "768", "items" => "3"),
                                $index.'_3' => array("breakpoint" => "1024", "items" => "5"),
                            ),
                        ],
                    ],
                ],
            ],
        ];
 
        /** @var \Magento\Config\Model\Config $configModel */
        $configModel = $this->configFactory->create(['data' => $configData]);
        $configModel->save();
    }
1
0

I have solved my issue. simply I have updated my config.xml like below. Now the default value is set for my system configuration

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>
            <image>
                <groupslide>
                    <small_image>{"_1517915932157_157":{"width":"400","height":"300","imagesize":"25600"}}</small_image>

                <image_file_types>jpg,jpeg,png,gif</image_file_types>
            </groupslide>

        </image>
    </default>
</config>
2
  • you can also add default value in system.xml too where defining the configuration. This is also another way to do the same. Commented Feb 7, 2018 at 10:34
  • how can I add it in system.xml?? Can you share any example
    – Jaisa
    Commented Feb 7, 2018 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.