Skip to content

Instantly share code, notes, and snippets.

@crittermike
Last active August 11, 2023 10:39
Show Gist options
  • Save crittermike/2d2c6734c506d509505fa79142125757 to your computer and use it in GitHub Desktop.
Save crittermike/2d2c6734c506d509505fa79142125757 to your computer and use it in GitHub Desktop.
Importing Drupal 8 config programmatically
<?php
// Import arbitrary config from a variable.
// Assumes $data has the data you want to import for this config.
$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 from an arbitrary file.
$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'));
@rang501
Copy link

rang501 commented Oct 5, 2020

It is not a good idea to write module config into active config directly, for example, it will erase some needed attributes, e.g _core and uuid which may cause issues later. It will also remove all other modifications by other modules if they are not imported into config (sometimes it is not possible due to dependencies like installation profile modules). Extra caution should be taken when updating config entities like fields, especially when creating them that way - db tables may not be created, entity API should be used instead.

My general advice when using this - update the parts you need not the whole configuration and when possible, use config entity object to change stuff.

@mike-potter
Copy link

That's the point of the snippet above: it is reading the actual config file from the config/sync folder, which contains the _core and uuid values already. Updating parts of a configuration is usually more fraught with danger. Importing config like this during an update hook is only a last resort however.

@rang501
Copy link

rang501 commented Oct 5, 2020

Sorry, I didn't specify, I meant the original snippet it just imports from module config, which doesn't (shouldn't) contain these values.
I just wanted to say something to other developers who will find this through Google search, that the original code might break stuff.

@bisonbleu
Copy link

bisonbleu commented Sep 8, 2021

@mike-potter I can't get your code to work: it seems the configs are not imported. Has anything changed since your post (Aug 19, 2020) that would account for that?
[update] The code works as suggested for simple configs e.g. filter.format.basic_html where I changed the name to 'BASIC HTML'. In my use case I'm trying to import a list of some 30+ configs. That's where my issue lies... maybe with unmet dependencies...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment