Magento 2 – Setting Default Value in Config.xml for Backend Tables

adminhtmlconfigurationmagento2table

To create a table configuration in Magento2 backend, we can use some good tutorial in here:
http://www.ibnab.com/en/blog/magento-2/magento-2-backend-guide-to-create-table-setting-in-the-system-configuration
or
https://magently.com/blog/magento-2-backend-configuration-frontend-model-part-33/

I read this tutorial in Magento 1 https://www.integer-net.com/how-to-create-tables-in-magento-system-configuration/.

They created a table configuration with default value. I knew that in Magento 1, we can set default array value for table in config.xml like that:

<default>
    <namespace_module>
        <settings>
            <shipping_costs>a:1:{s:18:"_1425462208957_957";a:2:{s:10:"from_price";s:1:"0";s:4:"cost";s:4:"4.90";}}</shipping_costs>
        </setting>
    </namespace_module>
</default>

I try to put the same array above from Magento 1 to config.xml file on Magento 2, but it does not work at all.

Here is my example:

system.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <tab id="media" translate="label" sortOrder="300">
        <label>MEDIA</label>
    </tab>
    <section id="payment" translate="label" type="text" sortOrder="402" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Settings</label>
        <tab>media</tab>            
        <group id="setting" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <field id="interest_rates" translate="label comment" sortOrder="13" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Interest Rates</label>
                <frontend_model>Company\Module\Block\Adminhtml\System\Config\Fieldset\InterestRates</frontend_model>
                <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>                    
            </field>
        </group>
    </section>  
<system>    

File InterestRates

protected function _prepareToRender()
{
    $this->addColumn('duration', ['label' => __('Duration'), 'style' => 'width:60px']);
    $this->addColumn('effective_interest_rate', ['label' => __('Effective Interest Rate'), 'style' => 'width:170px']);
    $this->addColumn('special_effective_interest_rate', ['label' => __('Special Effective Interest Rate'), 'style' => 'width:170px']);
    $this->_addAfter = false;
    $this->_addButtonLabel = __('Add Interest Rate');
}

Picture demo
enter image description here

I want to set default value for text column Duration, Effective_Interest_Rate and Special Effective Interest Rate

Can someone give me an advice? How can I set default value for table configuration in Magento .

Best Answer

In your case, you can follow the same Magento 1 way, you have to enter a serialized array there. From my experience, the best way to generate that serialized array is to fill in the default data in the backend, save it and then copy the value from core_config_data table to the config.xml.

For example config xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <setting>
                <interest_rates>a:4:{s:18:"_1477023122716_716";a:3:{s:4:"from";s:1:"0";s:2:"to";s:5:"10000";s:3:"fee";s:3:"324";}s:18:"_1477023174715_715";a:3:{s:4:"from";s:5:"10001";s:2:"to";s:5:"30000";s:3:"fee";s:3:"432";}s:18:"_1477023192619_619";a:3:{s:4:"from";s:5:"30001";s:2:"to";s:6:"100000";s:3:"fee";s:3:"648";}s:18:"_1477023219483_483";a:3:{s:4:"from";s:6:"100001";s:2:"to";s:6:"300000";s:3:"fee";s:4:"1080";}}</interest_rates>
            </setting>
        </payment>
    </default>
</config>

We can use this site http://www.unserialize.com/ to check our serialized array.