How to Set Configuration Values in Magento 2

configurationmagento2module

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?

Best Answer

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();
    }   
}