0

I made a module to add configuration fields to system (system.xml).

Some of these fields are dynamic arrays (cf here) which are generated using an extend of \Magento\Config\Model\Config\Backend\Serialized\ArraySerialized _afterload() function.

I would like to save these arrays programmatically.

How can it be done? (Saving the entire system configuration can be an option...)

Thanks for your help,

0

1 Answer 1

1

You can define your backend model to save data.

<backend_model><Namespace>\<Modulename>\Model\System\Config\Backend\SaveFileName</backend_model>

Create the file in the above path and you can use this logic to save or load data.

SaveFileName.php

namespace Namespace\Modulename\Model\System\Config\Backend;

class SaveFileName extends \Magento\Framework\App\Config\Value
{

    /**
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
     * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\Math\Random $mathRandom,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
    }

    /**
     * Prepare data before save
     *
     * @return $this
     */
    public function beforeSave()
    {
        $value = $this->getValue();
        $result = [];
        foreach ($value as $data) {
            //apply your logic here 
            //save your data in result array
        }
        $this->setValue(serialize($result)); // serialize your data
        return $this;
    }
}

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