Warning message

Documentation is currently being migrated into the new system. Some pages might be temporarily missing, and some guides might appear empty. Thank you for your patience while we are improving Drupal.org documentation.

Include default configuration in your Drupal 8 module

Last updated on
September 21, 2016 - 20:00

Main topic described: default configuration

Overview

In Drupal 7 default configuration for modules such as content types, field configuration, views, etc. required custom PHP code to install and keep updated. In Drupal 8, these are stored with the configuration system in plain text YAML files.

For example the configuration management system takes care of creating and managing content types. You can ship a content type with your module by creating a properly named and structured configuration file.

Example

Create a file named node.type.example_mytype.yml and place it in your module's directory in a subdirectory called config/install. For example this file could be at /modules/example/config/install/node.type.example_mytype.yml if the module is in /modules/example.

Please respect the naming-convention that you prefix your content type with your module name followed by an underscore to prevent conflicts with other configuration files.

Then you can place the following code in this file:

type: example_mytype
name: Example
description: 'Use <em>example</em> content to get to Drupal 8 development better.'
help: ''
new_revision: false
display_submitted: true
preview_mode: 1
status: true
langcode: en

See node.type.article.yml for a live example.

You can include default configuration for other things like block placement, views, text formats, editor configuration, user roles, etc. with your module the same way.

You can set default values for form fields like the node author as well.

If you installed the module before adding this content type, you'll need to uninstall and install it again for the configuration to take effect.

Exporting configuration

You can generate these files by exporting the configuration in Configuration > Development > Configuration synchronization. You can opt to use the "Single import/export" functionality to export a single piece of configuration, in which case you are presented with a text area containing the code to copy/paste into the configuration file. Or you can do a "Full export" which will let you download a tar.gz file that contains all configuration files in a single folder.

When using exported configuration it is important to remove the UUID from the configuration. Every piece of configuration is uniquely identified by this ID. If you would include this UUID in your module the ID would be the same for all sites that use your module, so it would not be unique any more. If you simply remove this line from the configuration Drupal will generate new UUIDs when your module is enabled, guaranteeing uniqueness.

uuid: 626187e6-2176-4a73-8900-c0912580e4a1 < < Remove this!
langcode: en
status: true

Drupal Console

The Drupal Console utility provides the following command:

It will perform the following tasks:

  • Export all configuration files associated with the specified content type (including its field configuration files).
  • Place it in the desired module's configuration directory.
  • Remove all of the problematic UUID lines.

So it saves a lot of work required by the manual process above.

See also