Drupal 8 Configuration Initiative (CMI)
Today we’re going to talk about a very nice feature introduced in Drupal 8, the CMI. When a website is being developed, you usually find yourself having to handle its configuration manually among all the environments you have. Even when you use features it’s common to encounter yourself constantly reverting them, recreating, comparing the differences and it’s an overwhelming process depending on how many features you have enabled on your website. Luckily the CMI has a much simpler way to handle this and it allows you to expose your configuration in a very readable way, YAML files.
How does it work?
In a very simple way to put it, Drupal will check your code and look for every configuration that can be exposed. If some is found, it will be exportable.
In Drupal 8, basically all the entities are exportable which means most of it is! Your views, image styles, block configuration among other entities can now be exported out-of-the-box with Drupal.
In depth, when you create a new module (for instance), you define a file called mymodule.schema.yml which basically holds the information of the configuration that can be exportable by Drupal. Here’s an example of a Lorem Ipsum example module:
To read more about the Schema YML file, click here.
Configuration files folder
By default, when you install a fresh copy of Drupal 8, after the installation steps, Drupal will add a new variable at the bottom of the settings.php file.
This is basically telling Drupal where to store the configuration files (.yml) that we’re going to export. By default, Drupal creates a hash-named folder to make it difficult to be accessed over the web. Although I’d strongly advise you to change that configuration, it’s safe to say you’re not at risk if you keep them.
Exporting the configuration
There are basically 3 easy ways of exporting the site’s configuration, I’ll leave to you to decision of which one is better for your needs.
Using Drush
This is my personal favourite. I use Drush heavily throughout my day on Drupal projects, it’s very simple and straightforward.
Open your terminal and type drush config-export .
This command will read the $config_directories[‘sync’] inside your settings.php and will prompt you to confirm wether you really want to export your configuration to that folder. By Pressing y and Enter it will override every configuration file you had previously on that folder.
Some useful shortcuts when using the command:
Remember you could combine multiple parameters within the same command run.
If you’d like to read more about Drush, check it here.
Using Drupal Console
Drupal console is another very powerful tool that helps you generate boilerplate code and other nice features, like debugging. Assuming you have it installed, simply run drupal config:export on your terminal to have your site’s configuration exported.
Using the Drupal UI
If you don’t like using the terminal, Drupal provides a very intuitive UI to handle it.
First, you’ll need to make sure you have the Configuration Manager module enabled. After that, access /admin/config/development/configuration (or under the admin navigation Configuration → Development → Configuration synchronization) and then click on the Export tab.
Once you reach this page, you’ll be presented with 2 sub-tabs. Those are, like their names say, to export a full archive of the configuration files or export them separately. Choose the best option for your site and we’ll cover in a few how you import them.
Importing the configuration
By now you’ve learnt how to export your site’s configuration so let’s review how we can import them.
Warning: That will override any configuration you had before. Use it carefully.
Using Drush
Again, this is my favourite option here.
Open your terminal and type drush config-import .
This command will read the $config_directories[‘sync’] inside your settings.php , look for every configuration file and import the configurations into Drupal.
Some useful shortcuts when using the command:
As mentioned before, you could combine multiple parameters within the same command run.
Using Drupal Console
Assuming you have Drupal Console installed, simply run drupal config:import on your terminal to have your site’s configuration imported.
Using the Drupal UI
If you used the Drupal UI for exporting your configuration, you’ll likely use it here as well.
Access /admin/config/development/configuration (or under the admin navigation Configuration → Development → Configuration synchronization) and then click on the Import tab.
Just like the Export page, you’ll be presented with 2 sub-tabs, Full archive and Single item. It’s up to you to decide if you’re importing a whole set of configuration of just some separated items.
What are the advantages of using it?
In a very short and direct answer: It’s fast. Other than being incredibly fast, the configuration is very well structured and split into different files that can be easily changed even if you’re not familiar with YML.
What about Features?
The Features module was completely rebuilt for Drupal 8 and it’s very powerful and flexible now. However, they have different roles in Drupal 8 now.
The CMI was mainly built to help you move your configuration across different environments of the same website while the Features module was built to help you leverage bundles and entities from one site to another. It really helps if you’re creating a new module that’s supposed to be generic.
Conclusion
By now you’re probably able to move your configuration across different environments and it should be pretty simple. Either if you’re using the built-in UI or the terminal, the steps we covered on the article should make you feel comfortable doing it.