Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Importing Drupal 8 config programmatically
<?php
// Import arbitrary config from a variable.
$config = \Drupal::service('config.factory')->getEditable('filter.format.basic_html');
$config->setData($data)->save();
// Or, re-import the default config for a module or profile, etc.
\Drupal::service('config.installer')->installDefaultConfig('module', 'my_custom_module');
// Or, import YAML config an arbitrary directory.
$config_path = drupal_get_path('module', 'my_custom_module') . '/config/install';
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
$config_storage->write('filter.format.basic_html', $source->read('filter.format.basic_html'));
@sumanchalki

This comment has been minimized.

Copy link

commented Jun 7, 2018

Good one.

@dlriley

This comment has been minimized.

Copy link

commented Oct 24, 2018

Huge thanks - saved a lot of time!

@JPustkuchen

This comment has been minimized.

Copy link

commented Jun 11, 2019

Very helpful, thank you!

You may also like this to install optional config e.g. in an update hook:

  $config_path = drupal_get_path('module', 'MY_MODULE') . '/config/optional';
  $config_source      = new \Drupal\Core\Config\FileStorage($config_path);
  \Drupal::service('config.installer')->installOptionalConfig($config_source);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.