11

Previously (in Magento 1, that is) we created a module called Setup which contained setting for the site. The upgrade scripts looks something like this:

$installer = $this;
$installer->startSetup();
$installer->setConfigData("fastsimpleimport/general/partial_indexing", 1);
$installer->setConfigData("fastsimpleimport/product/disable_preprocess_images", 1);
$installer->setConfigData('general/country/default', 'GB');
$installer->setConfigData('general/locale/firstday', 1); 
$installer->setConfigData('general/locale/timezone', 'Europe/London');
$installer->setConfigData('general/store_information/merchant_country', 'GB');
$installer->setConfigData('design/header/welcome', 'Enter your value');
$installer->setConfigData('design/head/title_suffix', 'Enter your value');
$installer->setConfigData('currency/options/base', 'GBP');
$installer->setConfigData('currency/options/default', 'GBP');
// ...

I can't figure out how to do the above in M2.

In other words: How do I go about setting config data in M2?

  • 2
    why do you need these in the DB? Can't you just add it in the config.xml file? – Marius Feb 23 '16 at 13:12
  • That's just the way things are done. It seems to be the common approach handling settings in module extensions (at least in M1). Also they need to be handled on a version basis so they can be migrated when our module is upgraded. – Martin Wickman Feb 23 '16 at 13:21
14

This seems to work:

class InstallData implements InstallDataInterface 
{
    public function __construct(
        LoggerInterface $loggerInterface,
        \Magento\Framework\App\Config\ConfigResource\ConfigInterface  $resourceConfig)
    {
        $this->logger = $loggerInterface;
        $this->resourceConfig = $resourceConfig;
    }        

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $this->resourceConfig->saveConfig(
            'a/b/c', 
            'value', 
            \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 
            \Magento\Store\Model\Store::DEFAULT_STORE_ID
        );

        $setup->endSetup();
    }   
}
  • 1
    I think it may also work (and be cleaner) to use interface from DI in the constructor for the $resourceConfig object. Like this: \Magento\Framework\App\Config\ConfigResource\ConfigInterface $resourceConfig – Hervé Guétin May 26 '16 at 13:49
  • Hey Martin, can you share some more of your module? I´m struggling in what namespace etc i need to use?! – Klettseb Jul 20 '16 at 15:56
  • why did you add logger also? just a habit or? – OZZIE May 24 '18 at 8:58
6

The following interface can be used \Magento\Framework\App\Config\Storage\WriterInterface, it has 2 methods:

  • save($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
  • delete($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)

It is more high-level compared to \Magento\Config\Model\ResourceModel\Config, and should be used from client code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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