0

I've got a custom module with a configuration file config/schema/entity.schema.yml. This configuration file is imported via an update hook :

  $active_storage = \Drupal::service('config.storage');
  foreach ($files as $file) {
    $active_storage->write($file, $source->read($file));
  }

This works well, and the configuration in that file gets imported.

However, during the subsequent run of drush cim drush insist on deleting that configuration. I assume that is because the entity.schema.yml does not exist in the root config/synch folder.

Is this the expected behaviour? How can I make Drupal not delete the configuration which is imported from the module's /config/schema folder?

3
  • 3
    Yes that's the expected behaviour - drush cex and commit before importing again
    – Clive
    Mar 16 at 13:26
  • @Clive OK, but exporting that configuration then duplicates the configuration to the root /config/synch folder. Should it really be located two places (module and root synch folders)?
    – sbrattla
    Mar 16 at 13:31
  • 3
    Yes it should be in both places - the module version is for install, the config/sync version if for syncing. Don't forget your config could (at least theoretically) diverge from what's in the module's code version, and those changes need to be pushed around too
    – Clive
    Mar 16 at 13:43
3

Yes this is the expected behavior. Config import and export is really quite simplistic (uncharitably: dumb). All it is doing when you run a config operation is to look at the active state of the DB and the active state of the sync directory and make one look like the other. On config:import (cim) you are saying "make the DB match those files in the sync directory". Export does the opposite. It's completely ignorant of other factors. So to make your changes "stick"

  1. enable the module
  2. run drush cex
  3. afterwards drush cim should show "no changes"

NOTE: you can override config in settings.php files by using `$config[my.config.file][a_key] = "my_value". This bypasses whatever the files say to do. This is very useful for per environment overrides or sensitive inputs like API keys.

A simpler approach over this manual writing you have is to add your config yml file to your_module/config/install/. When the module is installed this will be imported to the active config state. As commenters have mention when you then run drush config:export (cex) the files will be added to your config sync directory.

Note that your config in /install are essentially one time use. They should also not include the uuid in the top line of the file that is generated in cex built yml.

If you want to install config again from your /install directory you can accomplish this manually by running: drush cim -y --partial --source=modules/custom/my_module/config/install

--partial means do not remove any configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.