How to override configuration from a file? I have been playing a lot lately with an import configuration to Drupal 8. And I notice that all documentation show you how to edit value per value instead of overriding the entire config file. You can find more information about this at Configuration override system page.

I notice that DrupalConsole and Drush have the ability of overriding configuration by file.


Using Drush, you can run drush config-import
Using DrupalConsole, you can run drupal config-import


This will override the entire configuration base on the configuration file. How can I achieve this on the code? Hopefully, I will like to edit my configuration when I'm installation my module on hook_install.
To override config on code is simple. Above is an exemple how you get, set and save any config (that you just installed via .yml and from core).

On .install or .module file

  //Getting editable configs
  $module_cfg = \Drupal::configFactory()-&gt;getEditable('your.module.file');
  $settings = \Drupal::configFactory()-&gt;getEditable('any_core.settings');
  //Getting values
  $a_value = $settings-&gt;get('key_from_core');
  //Setting and save configs
  $module_cfg-&gt;set('any.key', $a_value)-&gt;save();
  $settings-&gt;set('key', 'new_value')-&gt;save();


From hook_install docs:


  Implementations of this hook are by convention declared in the
  module's .install file.


For further information see ConfigFactory docs.

There is a module called Features that helps if you want keep on track of your configs. I'm currently using this module for code-driven development, maybe is this what you're looking for.

Edit 1 - Replace entire config

First of all, you need to provide which config you pretend to override.

On my.custom.config.yml file  

# Make sure this config exists in your database
target: 'config.to.override'
... # all your data (CAUTION with 'dependencies')


On .install file

function module_install() {
  //Get your custom config
  $cfg = \Drupal::configFactory()-&gt;getEditable('my.custom.config');
  //Get config name to override
  $target = $cfg-&gt;get('target');
  //Get Config object
  $settings = \Drupal::configFactory()-&gt;getEditable($target);
  //Override all data that can be overridden
  $settings-&gt;setData($cfg-&gt;get())-&gt;save();
  //If you won't store any previous state, get rid of your custom config
  $cfg-&gt;delete();
}


This was tested in my development environment.