Drupal 8 Development, Part 1: Configuration Manager

This article is part of a series called Drupal 8 Development Deconstructed about custom development with Drupal 8.

Before we embark on developing our custom modules, let's take a minute to make a few changes to our Drupal development environment related to configuration management.

Configuration Management in D8

Drupal 8 has a whole new system for managing configuration data. The new system brings structure and consistency to the way configuration data is stored in D8, whether that configuration data is related to a core component, a contrib module, or a custom module you have written for your project. You can also export and deploy configuration settings from one environment to another much more easily than in D7.

By default, Drupal 8 stores configuration data in a table in the database; however you can easily change your setup so that configuration data is stored in files on your site. This makes it much easier to manage configuration settings via version control, and to track when changes are made. For our development environment, we are going to do just that, so that configuration data is stored in a physical directory, and loaded from a physical directory on our site.

Step 1: Create a directory outside the Drupal root to store our configuration data

In the project root directory (the one where the Vagrantfile is located), execute the following command to create our configuration directories:

mkdir config

Step 2: Set the location for the physical storage of our configuration files

By default, Drupal 8 configures a directory located inside sites/default/files for storage of exported configuration data. We are going to override this to move that location outside the web root, which is more secure and makes it easier to place these files under version control.

Edit "sites/default/settings.php", and add the following lines to the end of the file:

$config_directories = array(
         CONFIG_ACTIVE_DIRECTORY => '../config/active/',
         CONFIG_SYNC_DIRECTORY => '../config/sync/',
 );

Save the file.

Shell into your VM, navigate to the drupal root and execute "drush cr" to clear the cache.

Step 3: Export the current configuration

This step will get the current configuration data out of the database and into a physical file location.

  • Log into your site
  • Go to admin/config/development/configuration/full/export and click the "Export" button
  • Click on the "Import tab (admin/config/development/configuration/full/import) and upload the tarball you just exported.

Step 4: Copy the exported files directory

Go to the configuration directory created in Step 1, and execute the following command:

cp -r sync active

This will copy all of the configuration files located in the "sync" directory to the "active" directory.

Step 5: Create "services.yml"

Inside "sites/default" create a file named "services.yml" and paste in the following statements:

services:
  config.storage:
    class: Drupal\Core\Config\CachedStorage
    arguments: ['@config.storage.active', '@cache.config']
  config.storage.active:
    class: Drupal\Core\Config\FileStorage
    factory: Drupal\Core\Config\FileStorageFactory::getActive

Save the file.

Step 6: Reconfigure the site to use the configuration data stored in the "active" directory.

Edit "sites/default/settings.php", and add the following right before the lines added in Step 2:

$settings['bootstrap_config_storage'] = array('Drupal\Core\Config\BootstrapConfigStorageFactory', 'getFileStorage');

Save the file.

Shell into your VM and clear the cache: drush cr

-----
Your site will now load configuration data from the "active" directory instead of the database. These files can also be placed under version control if you wish to do so.

Comments

Has anyone trying going the reverse direction?

I have a D8 site with file-based config successfully enabled and working. I'd like to go back to database config storage but it doesn't seem to be as simple as undoing these edits to the settings.php and services.yml file (even when combined with a "drush cim sync").