Is it possible to inject a value from store config, or alternatively from a helper class (similar to m1 layout xml) into a class using di.xml?
1 Answer
According to this answer you can only inject something that will be interpreted as a helper method in layout files but not in di.xml.
I tried it with helper for di.xml and indeed it crashes.
I don't know if you can add a store config value, but here is an alternative of doing it.
make your class constructor accept a string parameter that will be the config path to the value you need and also add as a dependency an instance of \Magento\Framework\App\Config\ScopeConfigInterface.
protected $scopeConfig; protected $configPath; protected $yourConfigValue; public function __construct(, .... \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, $configPath, .... ) { $this->scopeConfig = $scopeConfig; $this->configPath = $configPath; $this->yourConfigValue = $scopeConfig->getValue($configPath); } If you are inside a block you don't need to add \Magento\Framework\App\Config\ScopeConfigInterface as a dependency. It is already added in the abstract block and you can access it as $this->_scopeConfig but only after you call parent::__construct().
Then you can add in di.xml this
<type name="your\class\here"> <arguments> <argument name="configPath" xsi:type="string">path/to/config</argument> </arguments> </type>