15

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
  • 2
    why do you need these in the DB? Can't you just add it in the config.xml file?
    – Marius
    Commented Feb 23, 2016 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. Commented Feb 23, 2016 at 13:21

2 Answers 2

18

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();
    }   
}
4
  • 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 Commented May 26, 2016 at 13:49
  • Hey Martin, can you share some more of your module? I´m struggling in what namespace etc i need to use?!
    – seb
    Commented Jul 20, 2016 at 15:56
  • why did you add logger also? just a habit or?
    – OZZIE
    Commented May 24, 2018 at 8:58
  • Definitely not required to add the logger as the InstallDataInterface does not provide a method signature for the constructor, so you are free to use any parameters you like there. As for the module itself: There is nothing else required besides the obligatory registration.php for this script to run during setup after the module has been enabled.
    – Louis B.
    Commented Oct 15, 2020 at 9:15
7

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 and acknowledge you have read our privacy policy.

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