Chapter 6. Adding Configuration Management to Your Module
After we learned how to access configuration objects and how schema files are structured in previous chapters, you will surely want to know how to get all this fancy stuff into your shiny new module for Drupal 8.
We will learn how to include the default configuration in custom modules, how to define and use your own configuration, and how to create configuration forms.
Let's start with a simple task and add some default configuration to our example module.
In Drupal 7, you have to use custom code to create and update the default configuration, such as content types, views, or field configurations. Many people also use the great Features module that provides some handy functions to manage default configuration easily.
Drupal 8 uses the .yml
files we talked about extensively in previous chapters to store information about the default configuration. The Configuration Management system itself takes care of creating and managing the default configuration, so we can focus on writing its definition rather than creating the functions for management.
Let's add a custom vocabulary to our site and define the term container and its configuration in the example module we used in the previous chapters. The vocabulary should be named Category
and have the internal identifier cm_example_category
(this is the machine-readable name). For the...
Defining and using your own configuration
Sometimes, including default configuration objects defined by other modules is not enough, and you may want to define your own configuration. As with many other parts of the Configuration Management in Drupal 8, this is not very complicated.
Setting your configuration file
Configuration files for configuration objects defined by a module reside in the config/install
subdirectory of this module. In our example, this is /modules/custom/cm_example/config/install
.
Tip
We put our example module in a directory named custom
inside the modules
directory to separate contributed modules downloaded from https://drupal.org from custom modules. This is a well-known best practice when creating sites to keep your modules organized.
In Chapter 3, Drupal 8's take on Configuration Management, we mentioned the naming conventions of configuration files. As said before, simple module settings should go into a file named <module name>.settings.yml
; thus, in our case...
As you can see, it is not very complicated to add a default configuration to your Drupal 8 module and to create a custom form for your configuration.
Drupal 8 provides you with powerful tools to define the form and create the required menu items, so you can focus on the form itself and give your users a good user experience.
Having to upgrade a configuration defined in previous versions, though, is not as simple. In the next chapter, we will describe how to upgrade your variables from older versions of Drupal to make them available with the new Configuration Management system.