سؤال

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>
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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();
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top