Nov 06, 2019 in Drupal 8, Snippets

One thing that bothers me quite a lot is the error when you try to import existing configuration into a new drupal 8 website. This would be the error you would traditionally get:

Error: Site UUID in source storage does not match the target storage

There is a drupal console argument that would say skip-validate-site-uuid when importing config, but that seems a bit buggy. Also, I could make some custom command with drush in a module, but I really wanted this to go automatically after installation and without patching the core. 

I resolved this by adding a custom global drush command that gets fired right before config import. So drush has some options to add post-hook and pre-hook commands, which is pretty cool. This finally was the best choice, now I really do not have to care about this uuid at all. 

How to

Place a file named CustomCommands.php in your <root>/drush/Commands folder:

namespace Drush\Commands;

class CustomCommands extends DrushCommands {
   * Sets a hardcoded site uuid right before `drush config:import`
   * @hook pre-command config:import
  public function setUuid() {
     $staticUuidIsSet = \Drupal::state()->get('static_uuid_is_set');
    if(!$staticUuidIsSet) {
      $config_factory = \Drupal::configFactory();
      $config_factory->getEditable('')->set('uuid', '6ba9bxxx-xxxx-438x-9c8x-e5x7x3d0x347')->save();
      drush_print('Setting the correct UUID for this project: done.');
      \Drupal::state()->set('static_uuid_is_set', 1);

Everything you now have to do is run:

drush config:import

This is great when you make use of devOps where you would uninstall and create drupal instances regularly. No need anymore to be looking for the original uuid to set it with drush, just let the magic happen automatically!