Include default configuration in your Drupal module

Last updated on
18 January 2024

Main topic described: default configuration

Overview

In Drupal 8 and later, configuration 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:  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
dependencies:
  module:
    - example
  enforced:
    module:
      - example

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. The dependencies and enforced keys ensure that the configuration is removed when the module is uninstalled.

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 anymore. 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

See also

Help improve this page

Page status: No known problems

You can: