Manage your Drupal 8 site configurations

Manage your Drupal 8 site configurations

06 Jul 2017 |  drupal

Deployment and configuration management are common actions of a project life cycle. Exporting and importing config files was very painful in Drupal 7 as everything was stored in the database without a built-in way to manage it. This resulted in the fact that each developer had his own secret sauce to manage configurations in between environments.

Nowadays, creating easy industrialized applications is very important and the community of developers working on Drupal 8 have fully understood this years ago. Today, Drupal 8 has an in-built in solution of managing configurations which allows you to export complete website configurations and store them in YAML files. Exported files can be imported to another website to get the same result. Drupal’s configuration system helps to solve the config files synchronization problem in two ways: a unified way to store configuration and a process to import/export changes between instances of the same site.

As a developer, it’s important to have an easy tool to achieve this, allowing us to move and share configurations between development and production sites.

Like Drupal 7, Drupal 8 configuration is stored in the database for performance and security reasons but it’s way more simple today to extract it. However, not everything is exportable, only the configuration not the contents.

Configuration management. What can be exported?

Before getting deep into the process of exporting/importing configuration, it’s important to understand what can be exported. Basically, everything that is configuration and not content. It’s a wrong idea to think that the configuration management system will help you to move content from a website to another one.

Thanks to the configuration management system, you can export:

  • site, modules and theme settings and states
  • content types, taxonomies with their associated settings
  • block types and settings
  • roles and permissions
  • fields, views …

However, the following things are considered as content and can’t be managed:

  • nodes
  • taxonomy terms
  • menu links
  • users
  • customer content entities

If you want to also manage contents from a website to another one, you should consider using the Migrate module and create your own migration classes.

How to synchronize config files

Once you have enabled the “Configuration Manager” core module, you can export your config files from the database to YML files. By default, Drupal will place these files under /sites/default/files/config_/sync but you can override these settings. From my point of view, it’s good practice to store config files outside of the web directory to avoid making them accessible from the Internet. You can easily change this settings by editing your settings.php file like this to move the config directory to the same level as the public files folder.

    $config_directories['sync'] = '../config/sync';

Note that the Configuration Manager module provides an admin interface available at /admin/config/development/configuration that allows you to import and export configuration files. As a developer, I strongly encourage you to use Drush to export your files instead of the admin UI.

Config files synchronization works in both directions. The export action allows you to generate YAML files from all of the configuration defined in the DB and the import action does the opposite by taking configurations from YAML into the database.

Exporting config files

From the admin UI, you can get a full archive of config files by clicking on the Export button (admin/config/development/configuration/full/export) and you can also do the same using Drush:

$ drush config-export

Importing config files

Importing config files with Drush is as simple as exporting:

$ drush config-import

In addition, you can add options to the drush config import command:

# Will only synchronize new and updated files. Deleted files won’t be synchronized to the database.
$ drush config-import --partial 
# It will display proposed changes before applying them like you would see using the git diff command
$ drush config-import --preview=diff 

Basic workflow summary

When doing Drupal 8 development, you will often export/import configuration files. These files are precious and should be versioned to your version control system.

To summarize, a basic workflow would be to export changes from your dev environment to the live one by following these steps:

On the development website:

  1. Enable the configuration management module, specify the config folder.
  2. Once you have done some work on your websites, export changes using the drush command.
drush config-export
  1. Review all the configuration files that have been updated. It’s important to do a good code review on these config files to ensure nothing has been deleted by mistake. Commit and push changes. On a live website:
  2. Do a DB backup. It’s strongly recommended that you do a database-dump before each synchronization. This could save your life on a potential needed rollback-strategy.
  3. Pull the project repository to get all new changes.
  4. Import changes to the live website.
drush config-import

Reload the website, it should have been synchronized with the config exported from the other environment in just a few steps.

Conclusion

Drupal 8 offers a built-in solution for exporting and importing site configurations which is way better than what you can do in D7. It’s not yet perfect, there is no standard approach, but the workflow described above is a simple and efficient solution. Your needs are maybe different and you may need to create your own workflow, if that’s the case, I’d love to hear about itr :)

Julien Dubreuil

Vous avez une idée, un projet web à réaliser ?

Ensemble, mettons en oeuvre sa réussite. Je vous accompagne dans vos projets, depuis l'élaboration du cahier des charges jusqu'à la mise en production. Pour plus d'information n'hésitez pas à me contacter.

Contactez-moi