In the world of Drupal module development, efficiently managing configuration is crucial. The Config API is designed to store and retrieve configuration data, ensuring your module's settings are persistent and manageable across different environments. This lesson explores how to store form data using the Config API, building on your previously acquired skills in creating module settings forms.
Understanding the Config API
The Config API in Drupal provides a standardized way to handle configuration data. Unlike content, which can be highly variable and user-generated, configuration data represents the constant, manageable settings like API keys or toggle switches that affect your site's behavior globally.
Setting Up Configuration Storage
Configuration is stored in YAML files within the config/install
directory of your module. This ensures that default configuration is initialized upon module installation.
Step 1: Define Default Configuration
Create a config/install
directory in your module, if it doesn't exist. Then, create a YAML file named my_rest_module.settings.yml
containing default settings:
api_key: 'default_api_key'
api_endpoint: 'http://default.endpoint.url'
These defaults will load when the module is first installed, ensuring a baseline configuration exists.
Step 2: Retrieve and Update Configurations
Integrate your module with the Config API by retrieving and modifying configurations as part of your form’s submit process. Here is how to access and store data:
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory->getEditable('my_rest_module.settings')
->set('api_key', $form_state->getValue('api_key'))
->set('api_endpoint', $form_state->getValue('api_endpoint'))
->save();
parent::submitForm($form, $form_state);
}
The getEditable()
method allows the configuration object to be modified, while save()
commits these changes to the configuration storage.
Accessing Configuration Values
Accessing stored configurations within other parts of your module is straightforward, using the Config API to load and use these values:
$config = $this->config('my_rest_module.settings');
$api_key = $config->get('api_key');
$api_endpoint = $config->get('api_endpoint');
With these configurations at your disposal, your module can dynamically adapt its behavior based on administrative settings.
Testing Configurations
To verify your configurations are stored correctly, submit your settings form and check that changes in configuration reflect immediately in your site. For debugging and confirmation, consider using Drush to inspect configurations directly:
drush config-get my_rest_module.settings
This command will output current configuration values, confirming they have persisted as expected.
Configuration Import and Export
Transporting configuration across different environments (such as development and production) is a key advantage of using the Config API. Use the following Drush commands to manage this process:
- Export Configuration:
drush config-export
- Import Configuration:
drush config-import
This allows seamless deployment of settings changes across environments, keeping various site configurations synchronized and manageable.
Conclusion
By effectively leveraging the Config API, you ensure that your module's settings are handled with precision and reliability. Proper configuration management not only enhances module stability but also streamlines deployment and maintenance processes across different site environments.
What’s Next?
Configuration is only as useful as it is reliable and validated. Our next topic will delve into Adding Validation to Config Forms, where we will ensure that user input meets necessary criteria before it's stored, maintaining data integrity and consistency.