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.