Including default config in config/installfor Drupal 8 , 9 , 10 , and 11

Last updated :  

As we progress through our module development series, understanding how to include default configuration in your module becomes crucial. This lesson will guide you through setting up default configurations in the config/install directory of your module, facilitating efficient module deployment and user onboarding.

Understanding Default Configuration

Default configuration is a set of initial settings that your module provides upon installation. This ensures that the module behaves predictably right from the start, supports seamless testing, and aids users in quickly getting started with its features.

Why Use Default Configuration?

Incorporating default configuration offers several benefits:

  • Ease of Use: Provides a baseline setup that users can modify according to their needs, reducing initial setup complexity.
  • Consistency: Guarantees consistent behavior across different environments and installations.
  • Efficiency: Streamlines the process of automation and testing with pre-defined configurations.

Example Scenario: Weather Module Default Config

Continuing with our weather module, let’s establish default configuration settings for the API key and default location.

Step 1: Create the config/install Directory

Within your module directory, create a config/install directory. This folder will store the YAML files defining your default settings.

Step 2: Define Default Configuration

Create a YAML file within this directory. The naming convention follows the format module_name.settings.yml, which in this case is weather_module.settings.yml.


// weather_module/config/install/weather_module.settings.yml

api_key: "your_default_api_key"
default_location: "New York"

Step 3: Understand the YAML Structure

The YAML file keys correspond to the configuration schema defined in your .schema.yml file. In this example, we've provided default values for api_key and default_location that align with the configuration schema discussed in prior lessons.

Step 4: Module Installation with Default Config

Upon installation, Drupal automatically imports these configuration files. If you enable the module using drush en weather_module or the module administration interface, these defaults are loaded into the active configuration store.

Verifying the Configuration Load

Navigate to the configuration settings form within Drupal's admin interface to ensure the default values are present. You can find this under /admin/config/weather_module/settings or wherever your module's settings form is linked.

Common Pitfalls

Here are a few common issues and how to avoid them when working with default configuration:

  • Mismatched Configuration Keys: Ensure that keys and structure in your YAML file match the configuration schema to prevent errors during module installation.
  • Configuration Overwrite: Be cautious when re-installing a module as defaults will overwrite existing active configuration unless import/export workflows are handled.
  • File Naming: Double-check that your file names follow the correct convention to ensure they are recognized and imported by Drupal.

Conclusion and Next Steps

In this lesson, you learned how to include default configuration in the config/install directory for your Drupal module. This process ensures predictable module behavior and simplifies the initial setup for users, significantly enhancing user experience and deployment efficiency.

Our next lesson will cover "Using hook_update_N() for configuration changes". This will help you manage configuration updates smoothly during module version upgrades, a crucial part of ongoing module maintenance.

Keep experimenting with these concepts, and I’ll see you in the next training session!