In Drupal module development, setting initial configuration values during the module installation process is crucial. Using the config/install
directory, you can ensure your module starts off with predefined settings, simplifying module deployment across different environments. This lesson will guide you through the process of including default configurations in your custom modules.
Understanding Default Configuration in Drupal
The config/install
directory is part of Drupal's configuration system, which allows you to define default configuration values for your module. These configurations are automatically imported when you first enable the module, ensuring consistent initial setup without requiring manual configuration adjustments. This is especially valuable for avoiding the "blank slate" problem where administrators have to initially configure everything manually.
Setting Up Default Configurations
To include default configurations in your module, follow the steps outlined below:
Step 1: Create the config/install Directory
First, ensure that your module has a config/install
directory. This is where your configuration files will reside. Use this command to create the directory if it doesn't exist:
mkdir -p modules/custom/my_rest_module/config/install
Step 2: Define Default YAML Configuration
Create a YAML file for each configuration item you wish to set default values for. Following the naming convention MODULENAME.SETTING.yml
, create a file named my_rest_module.settings.yml
within the config/install
directory. Here is an example content for setting default API configurations:
api_key: 'default_api_key_123'
api_endpoint: 'https://default.api.endpoint.com'
This YAML file contains key-value pairs representing default configuration settings for your module.
Step 3: Utilize the Configuration in Your Module
Once your default configurations are in place, they can be easily accessed in your module’s code. When your module is installed, these configurations are available through the Config API:
$config = \Drupal::config('my_rest_module.settings');
$api_key = $config->get('api_key');
$api_endpoint = $config->get('api_endpoint');
This approach allows your module to attain initial values automatically, without needing manual setup after enabling the module.
Handling Configurations Upon Module Update
It's important to consider the implications of default configurations when updating a module. Default values set in config/install
are only imported during the initial installation of the module, not when modules are updated or re-enabled. To handle updates efficiently, use configuration updates or schema changes.
For example, if a new default configuration is required after an update, you might use an update hook to apply changes:
function my_rest_module_update_8101() {
\Drupal::configFactory()->getEditable('my_rest_module.settings')
->set('new_config_key', 'default_value')
->save();
}
This ensures new configurations are added when a module is updated to a new schema version, without disturbing existing configurations.
Testing Your Default Configurations
To test your default configuration integration, reinstall your module. You can completely disable and then enable it, ensuring any default configurations are reapplied, or use Drush to uninstall and install:
drush pmu my_rest_module -y
drush en my_rest_module -y
Verify the default configuration is correctly set by retrieving it using the Config API or via Drush:
drush config-get my_rest_module.settings
Conclusion
By including default configurations through the config/install
directory, Drupal modules are equipped with the reliability and predictability needed for professional deployment and management. This minimizes errors, simplifies the initial setup, and contributes to seamless migration and updates across different environments.
What’s Next?
Having set reliable defaults, the next logical step is understanding how Drupal uses its powerful configuration management to facilitate efficient and consistent handling of these settings. Up next, we will investigate the Configuration Management Process in Drupal, enhancing your comprehension of enterprise-level configuration management strategies.