AN EFFECTIVE METHOD OF USING CONFIG SPLIT

If you use the Drupal 8 configuration system, then you know that one of the most difficult parts of using it effectively is managing configuration by the environment. Fortunately, the Config Split module makes it easy to manage different configurations in different environments, but how to configure it correctly is not always obvious. 

In this blog post, I will provide a method for configuring Config Split efficiently, with splits for local, remote development and production environments. 

The goal is to be able to set things up correctly the first time, without having to worry about manually enabling/disabling different "splits" depending on the environment. 

The key to everything is to take advantage of Config Split's ability to enable/disable individual splits with the settings in the settings.php file.

The first step is to configure your splits: I usually start by doing this in my local environment. Once Config Split is enabled, via the admin toolbar, go to Configuration | Development | Config Split Configuration (/admin/config/development/configuration/config-split). Click to add a new split configuration: in this example, we will use the following configuration:

  • Label: Local
  • Folder: ../config/splits/local
  • Active: (selected)
  • Modules: Complete split: Devel, Devel Generate, Devel Kint, Reroute emails
  • Configuration items: reroute_emails.settings

Let's also add a second split for remote development environments:

  • Label: Dev
  • Folder: ../config/splits/dev
  • Active: (selected)
  • Modules: full splits: redirect e-mails
  • Configuration items: reroute_emails.settings

Note that we are not going to split Config Split settings (I know), but we are going to allow local and development settings to be exported to the main /config/sync/ directory. 

Then, when exporting the configuration, the following directories and files will be created:

/config/splits/local/
    reroute_email.settings.yml
/config/splits/dev/
    reroute_email.settings.yml
/config/sync/
    ...
    config_split.config_split.local.yml
    config_split.config_split.dev.yml
    ...

At this point, if we did nothing else, the "Local" and "Dev" splits would automatically be active all the time in all environments and we wouldn't have accomplished anything yet. 

The last step to get everything working is to add a couple of lines of code to each environment's settings.php file. For example, in settings.local.php, add:

$config['config_split.config_split.local']['status'] = TRUE;
$config['config_split.config_split.dev']['status'] = FALSE;

Then, in settings.php for remote development environments, add:

$config['config_split.config_split.local']['status'] = FALSE;
$config['config_split.config_split.dev']['status'] = TRUE;

If you use a single settings.php for all environments, you can use if statements to determine the current environment and then enable/disable the appropriate splits. For example:

if ($env == 'local') {
  $config['config_split.config_split.local']['status'] = TRUE;
  $config['config_split.config_split.dev']['status'] = FALSE;
if ($env == 'dev') {
  $config['config_split.config_split.local']['status'] = FALSE;
  $config['config_split.config_split.dev']['status'] = TRUE;
else {
  $config['config_split.config_split.local']['status'] = FALSE;
  $config['config_split.config_split.dev']['status'] = FALSE;
}

With this type of Config Split configuration, you can always immediately run a configuration import after getting new commits from the project repository without having to worry about enabling or disabling your splits manually.

And this saves a lot of time and human errors in any Drupal project.

Have Any Project in Mind?

If you want to do something in Drupal maybe you can hire me.

Either for consulting, development or maintenance of Drupal websites.