Drupal - How do I update the configuration of a module?
As mentioned in the original question, and follow-up comments, there are a variety of contrib modules and manual methods to accomplish this.
To do it automatically, or in a custom fashion, I think hook_update_N()
is still probably the most viable option.
For instance, this is an example from the Head 2 Head to update system.site
to set the default_langcode
:
$config_factory = \Drupal::configFactory();
$langcode = $config_factory->get('system.site')->get('langcode');
$config_factory->getEditable('system.site')->set('default_langcode', $langcode)->save();
You can also read in config (recommended only for adding new config, not necessarily updating or overriding config that may have been customized):
$source = new FileStorage($path);
/** @var \Drupal\Core\Config\StorageInterface $active_storage */
$active_storage = \Drupal::service('config.storage');
$active_storage->write($name, $source->read($name));
where $path
is the absolute path to the my_config.foo.yml
file.
I've found this Gist on GitHub, which reverts/reloads given module's configuration using drush:
drush cim -y --partial --source=modules/path/to/module/config/install/
As I landed on this question as well but did not really find the correct answer for my situation here, I'd like to add another answer.
Please note: Anti-pattern ahead!
Use case
When we're developing projects we constantly update our test / acceptance environment with new configuration updates. Take for example a simple fictional News-module, we'd like to add a content-type to the module and deploy this to our acceptance environment. After review, we have concluded there are a few fields missing and other config-related stuff. Since we know the acceptance environment is not being updated in config, we really only want to reload the entire config from the module while added new functionality and not be bothered by importing every changed .yml
file.
We only need our config in modules when we're developing multisites. For single sites, we mostly just use exported site config in which the next step is unnecessary .
Reimport config entirely (anti-pattern!)
We found that using the ConfigInstaller service, we're able to reimport the complete config again from a specific module.
// Implement in a update_N hook.
\Drupal::service('config.installer')->installDefaultConfig('module', $module);
Use with caution!
I'd like to add that this will overwrite any active content that has been altered within the environment. So, only use this solution when you are sure it's safe to overwrite the active config. We'll never use this on a production environment and will only apply in early development.
First try out @jhedstrom's solution before you begin considering this one.