In Magento 2 you can only use a small set of config paths in the {{config}}
directive in blocks or pages.
The story behind it (scroll lower for solution):
The class that handles the {{...}}
directives for cms blocks and pages is Magento\Widget\Model\Template\Filter
. This one extends Magento\Email\Model\Template\Email
.
In this last class there is this method that handles {{config}}
directives
public function configDirective($construction)
{
$configValue = '';
$params = $this->getParameters($construction[2]);
$storeId = $this->getStoreId();
if (isset($params['path']) && $this->isAvailableConfigVariable($params['path'])) {
$configValue = $this->_scopeConfig->getValue(
$params['path'],
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
}
return $configValue;
}
In order to use a certain config path, the method isAvailableConfigVariable
needs to return true
This method looks like this:
private function isAvailableConfigVariable($variable)
{
return in_array(
$variable,
array_column($this->configVariables->getData(), 'value')
);
}
so it checks if the path you send is returned by \Magento\Email\Model\Source\Variables::getData
.
That's why only a small set of config paths can be used.
Now the solution
Create an after
plugin for the method \Magento\Email\Model\Source\Variables::getData
Add this to the di.xml
of one of your modules
<type name="Magento\Email\Model\Source\Variables">
<plugin name="add-test-credentials" type="[Vendor]\[Module]\Plugin\Model\Source\VariablePlugin" />
</type>
and create the plugin class:
<?php
namespace [Vendor]\[Module]\Plugin\Model\Source;
use Magento\Email\Model\Source\Variables;
class VariablePlugin
{
/**
* @param Variables $subject
* @param $data
* @return array
*/
public function afterGetData(Variables $subject, $data)
{
$data[] = [
'value' => 'testsection/settings/key',
'label' => __('Credentials for test settings') //give it a label that suits you best. This will be listed in the insert custom var popup in the cms pages and blocks (see image below)
];
return $data;
}
}
<div class="new class" data-sitekey="domain.com/admin/cms/wysiwyg/directive/___directive/e3tjdXN0b21WYXIgY29kZT1zaXRlX2tleX19/key/5a44f2ce7bbac0b884cde380826ded9c1448ad0957242f6c3b914e0489d95c67/"></div>
wheredata-sitekey
holds the field value. Its weird.