In our journey through Drupal module development, we've learned how to define configuration schemas using .schema.yml files. Now, we turn our focus to leveraging Drupal’s config.factory
service to save and manage module settings effectively.
Understanding Config.factory
The config.factory
service in Drupal is part of the configuration API, used to load, modify, and save configuration data. It standardizes how configuration data is handled, ensuring consistency and reliability across the system.
Why Use Config.factory?
Utilizing config.factory
offers several advantages:
- Centralized Management: Provides a centralized approach for managing configuration settings across your module.
- Data Consistency: Ensures that changes to configurations are consistent and safely saved.
- Seamless Integration: Integrates smoothly with Drupal’s administrative interfaces and configuration synchronization tools.
Example Scenario: Saving Weather Module Settings
Continuing with our weather module example, let’s implement the use of config.factory
to save API key and default location settings through a configuration form.
Step 1: Ensure Your Configuration Schema is Defined
Before using config.factory
, make sure your configuration schema is accurately defined in your .schema.yml
file, as discussed in the previous lesson.
Step 2: Implement a Configuration Form
Create a form to gather settings input from administrators. Each setting will be stored using the config.factory
service.
// weather_module/src/Form/WeatherSettingsForm.php
namespace Drupal\weather_module\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class WeatherSettingsForm extends ConfigFormBase {
protected function getEditableConfigNames() {
return ['weather_module.settings'];
}
public function getFormId() {
return 'weather_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('weather_module.settings');
$form['api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('API Key'),
'#default_value' => $config->get('api_key'),
];
$form['default_location'] = [
'#type' => 'textfield',
'#title' => $this->t('Default Location'),
'#default_value' => $config->get('default_location'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('weather_module.settings')
->set('api_key', $form_state->getValue('api_key'))
->set('default_location', $form_state->getValue('default_location'))
->save();
parent::submitForm($form, $form_state);
}
}
Step 3: Use Config.factory to Save Settings
The submitForm
method demonstrates the application of config.factory
. The config()
method retrieves the-specific configuration object. Using set()
, values for api_key
and default_location
are updated and subsequently saved with save()
.
Step 4: Display and Verify Settings
Enable your module and configure it via the new settings form in the admin interface. Ensure that changes are correctly applied and saved.
Common Pitfalls
Here are a few common mistakes and tips to avoid issues when using config.factory
:
- Schema-Mismatch: Always ensure your
.schema.yml
matches the configuration keys you are using withconfig.factory
. - Cache Clearance: Clear caches regularly using
drush cr
when changes do not reflect as expected. - Configuration Import: Be cautious during configuration imports to prevent overwriting unintended changes.
Conclusion and Next Steps
Successfully utilizing config.factory
allows you to manage module settings proficiently, maintaining configuration coherence and integrity. This skillset is indispensable for creating dynamic, feature-rich Drupal modules that cater to various administrative needs.
In our next lesson, we will explore "Retrieving configuration values programmatically", focusing on how to access and use configuration settings within your Drupal module logic effectively.
Continue learning, and I’ll see you in the next lesson!