Magento 2 - Can't save core config data value in custom module's ConfigPlugin

Viewed 781

I create a custom module on Magento 2 with admin form. When i save the config, i want to retrieve some info from an API.

I add a custom "admin_system_config_save_plugin" in di.xml The plugin is well called. My log is OK, but the config data is not saved in database :(

I've tried the save method of WriterInterface, direct query in sql (the query work perfectly in phpmyadmin) and the saveConfig of ressource

namespace Vendor\Module\Plugin;
use Magento\Framework\App\Config\ScopeConfigInterface;

class ConfigPlugin
{
    protected $_logger;
    protected $_helper;
    protected $_configWriter;
    protected $_curl;

    public function __construct(
                        \Vendor\Module\Logger\Logger $logger,
                        \Vendor\Module\Helper\Data $helper,
                        \Magento\Framework\HTTP\Client\Curl $curl,
                        \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
    ) {
        $this->_logger = $logger;
        $this->_helper = $helper;
        $this->_curl = $curl;
        $this->_configWriter = $configWriter;
    }
    public function aroundSave(
        \Magento\Config\Model\Config $subject,
        \Closure $proceed
    ) {
        $this->_logger->info("saveConfig");
        $this->getSmtpParams();
        return $proceed();
    }

    private function getSmtpParams() {
        try {
            $apiKey = $this->_helper->getApiKey();
            if ($apiKey) {
                $param = array();
                $param['module'] = 'magento';
                $url = "API_URL?api_token=".$apiKey;
                $this->_curl->post($url, $param);
                $cod = $this->_curl->getStatus();
                $res = $this->_curl->getBody();
                $this->_logger->info("Résultat: ".$cod);
                if ($cod == 200) {
                    $tmp = preg_split('/^\r?$/m', $res, 2);
                    $response = trim($tmp[0]);
                    $smtp_config = json_decode($response,true);
                    $this->_logger->info(print_r($smtp_config,true));
                    $this->_configWriter->save('idfuse/smtp/host', $smtp_config['host'], 'default', 0);
                    $this->_configWriter->save('idfuse/smtp/login', $smtp_config['username'], 'default', 0);
                    $this->_configWriter->save('idfuse/smtp/password', $smtp_config['password'], 'default', 0);
                }
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
    private function sendTestEmail()    {
        if(($this->_helper->isEnabled())&&($this->_helper->isTest()))   {

        }
    }
}
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Config\Model\Config">
        <plugin name="admin_system_config_save_plugin" type="ODL\Idfuse\Plugin\ConfigPlugin" sortOrder="1"/>
    </type>
</config>

I want the host, username and password saved in db :) The log is OK, so the function is correctly executed, but do nothing in core_config_data

0 Answers
Related