Handle configuration settings in custom Drupal modules and example of using the configuration API in Drupal

Read this article in:

Handling configuration settings in custom modules involves using the Configuration API provided by Drupal. This API allows you to define, manage, and access configuration settings in a structured and consistent way. Here’s how I approach it and an example of using the Configuration API in Drupal 10/11:

Using the Configuration API

Scenario: Let’s say we’re developing a custom module that needs to store and manage a setting for an API key. We’ll create a configuration schema, provide a default value, and create a configuration form to manage the setting.

  • Create Default Configuration File: Create a file named my_module.settings.yml in your module’s config/install directory:
api_key: 'default-api-key'
  • Define Configuration Schema: Create a my_module.schema.yml file in the config/schema directory to define the schema for your configuration:
my_module.settings:
  type: config_object
  label: 'My Module Settings'
  mapping:
    api_key:
      type: string
      label: 'API Key'
  • Read Configuration: Use the config service to read the configuration value:
$config = \Drupal::config('my_module.settings');
$api_key = $config->get('api_key');
  • Write Configuration: Use the config.factory service to save new configuration values:
\Drupal::service('config.factory')
  ->getEditable('my_module.settings')
  ->set('api_key', 'new-api-key')
  ->save();
  • Create Configuration Form. Create a form class in your module that extends ConfigFormBase:

namespace Drupal\my_module\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class MyModuleSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_module_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['my_module.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('my_module.settings');

    $form['api_key'] = [
      '#type' => 'textfield',
      '#title' => $this->t('API Key'),
      '#default_value' => $config->get('api_key'),
      '#description' => $this->t('Enter the API key used for connecting to the external service.'),
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('my_module.settings')
      ->set('api_key', $form_state->getValue('api_key'))
      ->save();

    parent::submitForm($form, $form_state);
  }
}
  • Add Route: Define a route in my_module.routing.yml to access the configuration form:
my_module.settings:
  path: '/admin/config/my-module/settings'
  defaults:
    _form: '\Drupal\my_module\Form\MyModuleSettingsForm'
    _title: 'My Module Settings'
  requirements:
    _permission: 'administer site configuration'

Summary

By following these steps, you ensure that your custom module’s configuration is managed in a consistent and secure way. The Configuration API provides a structured approach to handling settings, allowing you to define defaults, access and modify values programmatically, and create user-friendly forms for configuration management. This approach helps maintain a clean and maintainable codebase while providing flexibility for site administrators.

.^.

Please login to post comments: