I would like to create a custom config field and save it programatically using defined backend-model.
Context
I've tried to utilize configWriter, but it doesn't consider backendModel:
<?php namespace Company\Integration\Model; use Magento\Framework\App\Config\Storage\WriterInterface; class Setup { const API_KEY = 'paypal/wpp/api_signature'; public function __construct( WriterInterface $configWriter ){ $this->configWriter = $configWriter; } /** * {@inheritdoc} */ public function setup($apiKey) { $this->configWriter->save(self::API_KEY, $apiKey); } }
Result: The value is set in plain text, because it omits backend model logic
In above example, I'm saving paypal field, which is defined as encrypted. This is just a test scenario.
I was trying to trace back the way the config is saved via admin panel, but it seems too complex to reproduce.
Is there a proper way to do it, with accordance to backend model?
Hello @Hellward
Please try this , it will help you to set core config value by programming.
<?php
namespace Company\Integration\Model;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
class Data
{
protected $configWriter;
public function __construct(WriterInterface $configWriter) {
$this->configWriter = $configWriter;
}
/**
* @param $path = 'extensionName/general/data'
* @param $value = '1'
*/
public function SetData($path, $value) {
$this->configWriter->save($path, $value, $scope = ScopeConfigInterface:COPE_TYPE_DEFAULT, $scopeId = 0);
}
}
Hello @Hellward
Please try this code, it will help you to save data with core config value.
<?php
namespace Company\Integration\Model;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
class Data
{
protected $configWriter;
public function __construct(WriterInterface $configWriter) {
$this->configWriter = $configWriter;
}
/**
* @param $path = 'extensionName/general/data'
* @param $value = '1'
*/
public function SetData($path, $value) {
$this->configWriter->save($path, $value, $scope = ScopeConfigInterface:COPE_TYPE_DEFAULT, $scopeId = 0);
}
}
@Bhanu Periwal thanks for your answer, but unfortunately it is not helpful at all. The only difference from my code is setting default values directly, which makes no difference at all, because they are set on lower level anyways.
I do not have a problem with saving the config value itself. It is stored ok.
The problem is, that using this method, does not take into account the logic of backend model, defined in system.xml. In my case "Magento\Config\Model\Config\Backend\Encrypted".
The value is saved in plain text, even though it is configured to be encrypted.
I could encrypt the value myself, but I would prefer to use the backend model logic, for consistency.