2

Override configuration guide shows how users can override configuration with global $config variable, for example by using settings.php file (the same method as for D7).

But is it possible to override the configuration in a similar way but with yml file? It's much more convenient to use yml instead of php to override settings.

A real life example: let's say my module offers some configuration but I don't want to expose it via UI. I only want users to be able to change the settings via yml files (there are reasons for that). Obviously they shouldn't change my modules yml config file. Can they create a new yml file, name it accordingly (how?) and put inside sites dir?

3 Answers 3

1

No, you can't override with YAML files.

But, all configuration is YAML. You can edit them manually with single export and then import again, you can do a mass export and then change the exported files (then you even get a diff of the changes that you can confirm).

You can also use drush cedit yourmodule.settings or drush cset yourmodule.settings some.key the_value (or the export/import commands for editing configuration.

None of those touch the YAML files in your module.. those are only the starting point and are then imported into the active config store of a site, after which they can be edit with the mentioned tools.

1
  • 1
    Too bad. That would be very convenient. Jun 23, 2015 at 20:31
1

No, it's not possible with YAML files but there is another way to override a configuration apart from the global overriding system.

Global overrides

global $config;
$config['system.maintenance']['message'] = 'Custom maintenance message.';


Module overrides

1] Create a service with 'config.factory.override' tag.

services:
  foo.config_overrider:
    class: \Drupal\foo\FooConfigOverrides
    tags:
    - { name: config.factory.override, priority: 5 }


2] Create a service class in your module root directory.

namespace Drupal\foo;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;

class FooConfigOverrides implements ConfigFactoryOverrideInterface {

  /**
  * {@inheritdoc}
  */
  public function loadOverrides($names) {
    $overrides = [];

    if (in_array('system.maintenance', $names)) {
      $overrides['system.maintenance'] = ['message' => 'Custom maintennace message.'];
    }

    return $overrides;
  }

  /**
  * {@inheritdoc}
  */
  public function getCacheSuffix() {
    return 'FooConfigOverrider';
  }

  /**
  * {@inheritdoc}
  */
  public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
    return NULL;
  }

  /**
  * {@inheritdoc}
  */
  public function getCacheableMetadata($name) {
    return new CacheableMetadata();
  }
}


  • loadOverrides() Provide overridden configuration values
  • getCacheSuffix() Return a string to be used in the static cache identifier
  • createConfigObject() Create a configuration API object that would be used during installation or synchronization
  • getCacheableMetadata() Return any cache metadata related to our override


References

0

It is possible to override config with YAML files in case you use your own installation profile. This option is provided by Drupal core, no additional modules are needed. It can override any configuration from core or contrib module. It fully replaces the original yml file, therefore "partial" override is not possible. The overrides can be added any time, not only when the site is installed.

For example profiles/myprofile/config/install/system.site.yml will override the site config from the system module. It is quite easy to write your own install profile, this feature should be better documented.

There are now multiple contrib modules which provide (even partial) config overrides with yaml files.

config_override, config_rewrite, config_replace

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.