Config Importer and Tools

Component ID

2699595

Component name

Config Importer and Tools

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

11222

Component created

Component changed

Component body

This module aims to cover development needs with managing configurations during project development.

Importing all configs during development is not convenient and can lead to a bad behaviour with overwriting or deleting configs or content from a site.

So there is a need for importing only of specific configs via hook_update_N().

Usage

All the work happening with the config_import.importer service. Let's instantiate it:

/* @var Drupalconfig_importConfigImporterServiceInterface $config_importer */
$config_importer = Drupal::service('config_import.importer');

By default, import and export operations will use the sync directory. But, if needed, it could be changed to a path of existing directory or type of already configured configuration directories. For instance:

// $config_importer->setDirectory(CONFIG_STAGING_DIRECTORY);
$config_importer->setDirectory('/var/config');

You may do so to import existing configs:

$config_importer->importConfigs(['core.extension']);

And export can be achieved with a similar construction:

$config_importer->exportConfigs(['core.extension']);

Features integration

To revert/import features you have to enable the features module first:

drush en features -y

Get instance of service:

/* @var Drupalconfig_importConfigFeaturesImporterServiceInterface $features_importer */
$features_importer = Drupal::service('config_import.features_importer');

And do the import:

$features_importer->importFeatures(['feature1', 'feature2']);

Drush integration

Execute the next command to see the list of available commands from a group:

drush help --filter=config_import

Then use the following syntax to find out more information about concrete command:

drush help COMMAND_NAME

Example use cases

/**
 * Revert custom block feature.
 */
function ygh_master_update_8024() {
  Drupal::service('config_import.features_importer')->importFeatures(['openy_block_custom_simple']);
}

/**
 * Update HTML format config.
 */
function ygh_master_update_8025() {
  /* @var Drupalconfig_importConfigImporterServiceInterface $config_importer */
  $config_importer = Drupal::service('config_import.importer');
  $config_importer->setDirectory(YGH_CONFDIR);
  $config_importer->importConfigs(['filter.format.html']);
}

To do

  • Create UI for making manual config changes convenient

Video about this module: https://www.youtube.com/watch?v=vFsgNjhGr4Y

For updating specific property in config:

1) go to related to this config module

2) create new hook_update_N in openy_*.install file

3) in update add next code (this is example):

$config = drupal_get_path('module', 'openy_media_image') . '/config/install/views.view.images_library.yml';
$config_importer = Drupal::service('config_import.param_updater');
$config_importer->update($config, 'views.view.images_library', 'display.default.display_options.pager');

Where:

  • $config variable contains path to config with config name
  • "views.view.images_library" - config name
  • "display.default.display_options.pager" - config specific property (you can set value from a nested array with variable depth)