Skip to content

Instantly share code, notes, and snippets.

@vasi
Last active November 11, 2021 15:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasi/c0bf084e270213927e6fcbff1c149d5c to your computer and use it in GitHub Desktop.
Save vasi/c0bf084e270213927e6fcbff1c149d5c to your computer and use it in GitHub Desktop.
Modify Drupal's config UUIDs to match a config dir
<?php
use Symfony\Component\Yaml\Yaml;
global $config_directories;
$config_name = drush_shift();
if (!isset($config_directories[$config_name])) {
print "No such config directory $config_name\n";
exit;
}
$config_dir = $config_directories[$config_name];
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = \Drupal::service('config.factory');
$names = $config_factory->listAll();
$names = array_combine($names, $names);
$ymls = file_scan_directory($config_dir, '/\.yml$/', ['recurse' => FALSE]);
$change = [];
foreach ($ymls as $yml) {
$content = Yaml::parse(file_get_contents($yml->uri));
if (isset($content['uuid']) && isset($names[$yml->name])) {
printf("Modifying %s\n", $yml->name);
$config = $config_factory->getEditable($yml->name);
$config->set('uuid', $content['uuid']);
$config->save();
}
}
@vasi
Copy link
Author

vasi commented Mar 17, 2017

If you try to import Drupal 8 CMI config into another site, it will try to delete and recreate all the config items, because of UUID mismatches. The import will then fail due to things like nodes being dependent on node type config. This makes it difficult to have multiple people work together on a site without sharing a database.

This script takes a CMI config directory as input, and changes the UUIDs of Drupal's config to match. A subsequent config import should succeed, updated config instead of deleting and recreating it.

To use:

drush php-script uuid.php sync
drush config-import sync

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