Magento 2-无法将核心配置数据值保存在自定义模块的ConfigPlugin中

时间:2019-06-25 10:04:56

标签: php magento2

我用管理表单在Magento 2上创建了一个自定义模块。 保存配置时,我想从API检索一些信息。

我在di.xml中添加了自定义“ admin_system_config_save_plugin” 该插件很好称呼。我的日志还可以,但是配置数据未保存在数据库中:(

我尝试了WriterInterface的保存方法,sql中的直接查询(该查询在phpmyadmin中完美运行)和ressource的saveConfig

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>

我希望主机,用户名和密码保存在db中:) 日志正常,因此该函数已正确执行,但在core_config_data中不执行任何操作

0 个答案:

没有答案