cancel
Showing results for 
Search instead for 
Did you mean: 

Save custom config, using deifned backend model programatically

Save custom config, using deifned backend model programatically

I would like to create a custom config field and save it programatically using defined backend-model.

Context

  • Field is meant to be encrypted.
  • Possibly validated via config
  • Will be set via extended magento api method
  • Config won't be accessible from admin panel 

 

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?

3 REPLIES 3

Re: Save custom config, using deifned backend model programatically

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:Smiley FrustratedCOPE_TYPE_DEFAULT, $scopeId = 0);
   }
}

Re: Save custom config, using deifned backend model programatically

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:Smiley FrustratedCOPE_TYPE_DEFAULT, $scopeId = 0);
   }
}

Problem solved? Click Accept as Solution!

Re: Save custom config, using deifned backend model programatically

@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.