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>
  • Do you want to create a new system configuration value setting ? – MGento Feb 5 '18 at 10:08
  • @MGento, I have updated my question Please answer – Jaisa Feb 5 '18 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 Feb 5 '18 at 10:14
  • 1
    If user failed to enter dimensions means , Default dimensions will work. – Jaisa Feb 5 '18 at 10:22
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.

  • See the updated post ans answer.Please correct me if i am wrong – Jaisa Feb 7 '18 at 8:45
0

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();
    }

Complete module code and detailed explanation can be found here - Working with ArraySerialized backend model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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