1

I'm making a plugin for the Magento\Framework\App\Config\ScopeConfigInterface file.

I need to get the config value from database.

Right now, I manage to accomplish this, but I'm having an issue with obscured fields.

I'm getting the value that comes directly from database.

If I inject the EncryptorInterface I can decrypt it, but the problem is that I need to know when is necessary to decrypt and when should I need to return the value from database.

I have at this point of the flow the path for the config option (example: carriers/usps/userid )

I was looking for a way to detect the backend model for the field using the path to load the model.

I tried something like this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$field = $objectManager->create('Magento\Config\Model\Config\Structure')->getElement($path);

And then try this to get the backend Model:

    if ($field->hasBackendModel()) {
        $backendModel = $field->getBackendModel();
        //backend model is empty
    }

Not sure how I can achieve to detect if the field has backend model Magento\Config\Model\Config\Backend\Encrypted

Is there some way to read the xml tree, and using the field path get the backend model, so I can know if I should use decrypt for returning the value or not?

2 Answers 2

1

Magento\Framework\App\Config\Initial->getMetaData() returns an array of all config paths with a backend model that requires processing (decrypting) before being written to cache.

Specifically, the below code will give you a clean array to reference a given config path against to see if it needs decrypting.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$metaData = $objectManager->create('Magento\Framework\App\Config\Initial')->getMetaData();

$pathsRequiringDecryption = [];

foreach ($metaData as $path => $processor) {
    if ($processor['backendModel'] == 'Magento\Config\Model\Config\Backend\Encrypted') {
        $pathsRequiringDecryption[] = $path;
    }
}

Obviously use proper DI rather than the object manager in production yada yada you know the deal.

0

You need to add backend_model in config.xml for those filed which type obscure.

Ex:

<field id="account" translate="label comment" type="obscure" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="0">
    <label>Account ID</label>
    <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>

You need to add in confix.xml like:

<account backend_model="Magento\Config\Model\Config\Backend\Encrypted">TEST</account>

For more detail check dhl module

Now when you use the following code, Magento will return actual value not encrypted value:

$this->scopeConfig->getValue('yoursection/yourgroup/yourfield')
1
  • Hi Sohel! I know how to use an encrypted field, but what I need to solve is to get an already existing field, using the path, and see somehow if it should be decrypted or not Commented Apr 1, 2019 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.