Description
In Drupal, site settings are the basic configuration options that define how your website works and behaves. These settings include things like site name, site slogan, email address, timezone, and more. They can be accessed and modified by administrators from the Drupal admin dashboard.
One useful feature of Drupal is the ability to add custom fields to your site settings. This allows you to collect and store additional information about your site that is not included in the default settings.
To add a custom field to your Drupal site settings, you first need to create a new field using the "Field UI" module. This module provides a graphical interface for adding and managing fields on your site.
Once you have created your custom field, you can then add it to your site settings form using the "Form API" in Drupal. This allows you to define how the field is displayed and processed in your site settings form, including validation and sanitization.
Some common examples of custom fields that can be added to Drupal site settings include additional contact information, site metadata, and branding elements like logos or images.
Overall, adding custom fields to your Drupal site settings is a powerful way to extend the functionality and flexibility of your website, allowing you to collect and store more information and tailor the user experience to your specific needs.
One useful feature of Drupal is the ability to add custom fields to your site settings. This allows you to collect and store additional information about your site that is not included in the default settings.
To add a custom field to your Drupal site settings, you first need to create a new field using the "Field UI" module. This module provides a graphical interface for adding and managing fields on your site.
Once you have created your custom field, you can then add it to your site settings form using the "Form API" in Drupal. This allows you to define how the field is displayed and processed in your site settings form, including validation and sanitization.
Some common examples of custom fields that can be added to Drupal site settings include additional contact information, site metadata, and branding elements like logos or images.
Overall, adding custom fields to your Drupal site settings is a powerful way to extend the functionality and flexibility of your website, allowing you to collect and store more information and tailor the user experience to your specific needs.
Keywords
Custom field in basic site settings page
For example, you have to add a custom field to "Basic site settings" page (/admin/config/system/site-information).
Let's say it is a phone number of your site.

For this new field you have to implement hook hook_config_schema_info_alter() form in *.module file.
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_config_schema_info_alter().
*/
function MODULENAME_config_schema_info_alter(array &$definitions): void {
$definitions['system.site']['mapping']['phone'] = [
'type' => 'string',
'label' => t('Site phone'),
];
}
/**
* Implements hook_form_system_site_information_settings_alter().
*/
function MODULENAME_form_system_site_information_settings_alter(array &$form, FormStateInterface $form_state): void {
$form['site_information']['phone'] = [
'#type' => 'textfield',
'#title' => t('Phone'),
'#default_value' => \Drupal::config('system.site')->get('phone'),
];
$form['#submit'][] = 'MODULENAME_form_system_site_information_settings_submit';
}
/**
* Implements hook_form_system_site_information_settings_submit().
*/
function MODULENAME_form_system_site_information_settings_submit(array &$form, FormStateInterface $form_state): void {
\Drupal::configFactory()->getEditable('system.site')
->set('phone', $form_state->getValue('phone'))
->save();
}
Also, it can be helpful create a token for this phone field in the same *.module file.
<?php
/**
* Implements hook_token_info().
*/
function MODULENAME_token_info(): array {
$info['tokens']['site']['phone'] = [
'name' => t('Phone'),
'description' => t('Site phone.'),
];
return $info;
}
/**
* Implements hook_tokens().
*/
function MODULENAME_tokens(string $type, array $tokens): array {
$replacements = [];
if ($type === 'site') {
foreach ($tokens as $name => $original) {
if ($name === 'phone') {
$replacements[$original] = \Drupal::config('system.site')->get('phone');
}
}
}
return $replacements;
}
So, this is it.
Subtitle
Custom field in basic site settings page