One of the best tools for a good configuration management in Drupal is configuration split. In Drupal 8+ exporting and importing site configurations to other sites is very common, the problem comes when developers have development functions enabled that they do not want or should not bring to production.
A clear example of this would be to have the devel module enabled in the development environment but it is not required to have it also in production, Configuration Split helps to create a separate directory where these modules can be imported to a specific environment and to not be added to production if it's not required. Another example would be to assume that we have a site about web development tools, this site will be a multisite and it will be necessary for all the other sites to have the same basic configuration, the difference would be that some of these sites will use content types that others they will not use, for example, Pages, FAQs or Bloqs. Note that this module does not interfere with the Drupal configuration, but rather filters the import/export pipeline.
Using this module you can define the configuration sets that are required to be exported and imported, thus avoiding having modules or unnecessary information. It also turns out to be a very handy tool when working with multisites.
Run the following command to add the module:
$ composer require drupal/config_split
Enable the module with this command:
$ drush en config_split
Creation of the configution split configuration
- Go to Admin>Configuration>Development>Configuration Split Settings.
- Click on Add Configuration Split Setting.
- Add a Label.
- In the Folder Field option, add the name of the folder.
- Save it.
Add the configuration
onfiguration Split provides 2 methods to split the configuration: complete and conditional.
Complete split: this method takes care of removing the configuration from the config/sync directory and puts it only in the previously created split directory.
Conditional Split: This method keeps the settings in the config/sync directory, but allows a split if it is different from the one in the split directory.
- Go to Admin>Configuration>Development>Configuration Split Settings and the select the Split to edit.
- Under Complete Split select the modules and items that only will be available when the split it enable.
- Same process for Conditional Split
- Save it.
Enable and Export the Split
Once the split has been created it is necessary to activate it in the site's settings.php file, by adding the following line of code:
$config['config_split.config_split."name_of_split"']['status'] = TRUE;
Where name_of_split is the machine name of the recently created split.
Run drush cex to export the configuration.
If we have a development split this must be enabled only for the development environment in the local file of settings.local.php. as follows:
$config['config_split.config_split.development']['status'] = TRUE;
And in production this code is not present in the settings.php file but it can also be disabled by adding the following code:
$config['config_split.config_split.development']['status'] = FALSE;