site_config
Provides a centralized way to define, manage, and expose global site configuration in Drupal, with first-class support for decoupled or headless architectures.
This module allows developers to define structured configuration groups via plugins, automatically generates an administration UI, and exposes the stored values through services and optional API endpoints (REST and JSON:API).
Features
SiteConfig plugin type
This module introduces a new plugin type called SiteConfig, which allows you to define logical groups of configuration fields using annotations.
Each plugin represents a configuration set with:
- A unique ID
- A label and description
- A list of fields with type, label, options, and validation
- Optional storage and translation settings
Plugins must be created under:
src/Plugin/SiteConfigExample SiteConfig plugin
Define the Plugin using PHP Attributes (Recommended)
namespace Drupal\your_module\Plugin\SiteConfig;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\site_config\Attribute\SiteConfig;
use Drupal\site_config\SiteConfigPluginBase;
#[SiteConfig(
id: "custom_settings_attribute",
label: new TranslatableMarkup("Custom Settings attribute"),
fields: [
"welcome_message" => [
"type" => "textfield",
"title" => "Welcome Message",
"description" => "A message shown on the home page.",
],
"featured_node" => [
"type" => "entity_autocomplete",
"target_type" => "node",
"title" => "Featured Content",
],
],
storage: "config",
translatable: true,
)]
class YourPluginName extends SiteConfigPluginBase {
}
Define the Plugin using Annotations (Legacy)
namespace Drupal\site_config\Plugin\SiteConfig;
use Drupal\site_config\SiteConfigPluginBase;
/**
* Plugin implementation of a SiteConfig.
*
* @SiteConfig(
* id = "foo",
* label = @Translation("Foo"),
* description = @Translation("Foo description."),
* fields = {
* "field_1" = {
* "label" = @Translation("Field 1"),
* "type" = "select",
* "options" = { 0 = "No", 1 = "Yes" },
* "required" = TRUE
* },
* "field_2" = {
* "label" = @Translation("Field 2")
* }
* },
* storage = "config",
* translatable = TRUE
* )
*/
class Foo extends SiteConfigPluginBase {
}
Plugin options
- storage (optional): determines where the values are stored.
Possible values:state,config.
Default:state - translatable (optional): enables translation support for configuration values.
Default:FALSE - field type (optional): defaults to
textfieldif not specified.
Automatically generated admin form
The module provides an administration page at:
/admin/site-config
This form is generated automatically based on the defined SiteConfig plugins and handles storing and retrieving their values.
API submodules
The module includes two optional submodules to expose configuration values for decoupled frontends.
site_config_jsonapi
Provides JSON:API endpoints:
- Get all site configs:
/jsonapi/site-config - Get a specific config by ID:
/jsonapi/site-config/item/{id}
site_config_rest
Provides REST endpoints:
- Get all site configs:
/api/site-config - Get a specific config by ID:
/api/site-config/{id}
Services
The module defines the SiteConfigService, which allows programmatic access to configuration values:
\Drupal::service('site_config.service')->getSiteConfig(); Returns all configuration values.
\Drupal::service('site_config.service')->getSiteConfigById('foo'); Returns all values for a specific SiteConfig plugin.
\Drupal::service('site_config.service')->getValue('foo', 'field_1'); Returns a single field value.
\Drupal::service('site_config.service')->setValue('foo', 'field_1', 'value'); Sets a field value programmatically.