Я создаю пользовательский модуль в Magento 2 с формой администратора.Когда я сохраняю конфигурацию, я хочу получить некоторую информацию из API.
Я добавляю пользовательский "admin_system_config_save_plugin" в di.xml. Плагин хорошо называется.Мой журнал в порядке, но данные конфигурации не сохраняются в базе данных: (
Я пробовал метод сохранения WriterInterface, прямой запрос в sql (запрос отлично работает в phpmyadmin) и saveConfig из 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>
Я хочу, чтобы хост, имя пользователя и пароль были сохранены в db :) Журнал в порядке, поэтому функция выполняется правильно, но ничего не происходит в core_config_data