Last active
November 11, 2021 15:23
-
-
Save vasi/c0bf084e270213927e6fcbff1c149d5c to your computer and use it in GitHub Desktop.
Modify Drupal's config UUIDs to match a config dir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: