Specbee: How to split configurations across different sites in Drupal 10

Configuration management is one of the best features introduced in Drupal. It allows developers to easily push configuration changes from development to staging, and finally to production environments. However, some configurations are environment-specific. For instance, modules like Devel, Kint, Views UI, and Google Tag are only enabled in development environments and not in production. Fortunately, the Configuration Split module offers a solution by storing configurations in a separate directory, allowing for environment-specific imports. In this article, you'll learn how to split configurations across different websites using this powerful Drupal 10 module. Setup and using the Configuration Split module Installing the Drupal Configuration Split module is like installing any other contributed module. Use composer to install it since it automatically installs all the necessary dependencies. Open the terminal, within the project and enter the command. $ composer require drupal/config_splitCreate the split configuration Once installed and enabled, we can create one or more "splits" to keep our configuration file in a separate folder. Go to Admin > Configuration > Development > Configuration Split Settings Click Add Configuration Split Setting Enter a Label In the folder field, enter the folder name relative to the Docroot. The path will specify the folder inside which the split configurations should be stored. ../config/dev_splitMake sure the machine name of your split is the same as the folder name. You can keep the split active or inactive by default. These settings can be overridden by settings.php. Choose the module you want to split. In our case – the Devel Module. Since we are pushing the module to a separate config split folder, We have to partially split core.extension.yml file, which stores information about what modules must be installed on your site. Click Save. The config files of the selected module will also be sent to the same folder once you export the config split. The module also enables users to select any particular config file to be split. Activate a Split Once the split is created, it needs to be activated to carry out a split. The Drupal 10 Configuration Split module does not provide a UI for this purpose, but instead, we can modify our settings.php file to activate the split: $config['config_split.config_split.dev_split']['status'] = TRUE;Where, dev_split is the machine name of the split we created earlier. Now, export your configuration using drush cex. You can see the config_split settings getting updated and the module getting removed from your core.extension file, along with respective settings files. To export the configs selected in the dev_split, you have to run a different command, i.e. drush config-split: export “split_name”In our case it would be, drush config-split:export dev_split. Now you can see the files selected in dev_split getting exported to the dev_split directory.  For our Development split, we need to have it activated in the development environment, but not in production. To do so, we add the following to our settings.php on our development environment. $config['config_split.config_split.development']['status'] = TRUE; For the Production site we won't add this code in the settings file, or we can also disable it explicitly by using below code: config['config_split.config_split.development']['status'] = FALSE;Activate split based on environment You can also specify which split should be active in a certain environment by adding a condition in settings.php as shown below: if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {    switch ($_ENV['AH_SITE_ENVIRONMENT'])    {      case 'develop':     $config['config_split.config_split.dev_split']['status'] = TRUE;     break;      case 'live':     $config['config_split.config_split.prod_split']['status'] = TRUE;     break;    }  }The above code will activate dev_split in the development (‘develop’) environment and prod_split in the production (‘live’) environment. Final Thoughts The Configuration Split Module is a fantastic feature introduced in Drupal’s configuration management. By splitting up configurations based on environments, you can use the module only in certain environments, based on your needs. We hope you found this article helpful. For more interesting articles on Drupal and everything technology, please bookmark our blog and come back for more!
PubDate

Tags