improvements-2.x-dev/modules/improvements_site_information/improvements_site_information.module

modules/improvements_site_information/improvements_site_information.module
<?php

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_config_schema_info_alter().
 */
function improvements_site_information_config_schema_info_alter(array &$definitions): void {
  $definitions['system.site']['mapping']['improvements'] = [
    'type' => 'sequence',
    'label' => 'Third-party site information',
    'sequence' => [
      'type' => 'site_third_party_setting.[%key]',
    ],
  ];
}

/**
 * Implements hook_site_third_party_settings_info().
 */
function improvements_site_information_site_third_party_settings_info(): array {
  return [
    'company_name' => [
      'title' => t('Company name'),
      'form_element_type' => 'textfield',
      'weight' => 0,
    ],
    'company_mail' => [
      'title' => t('Company e-mail address'),
      'description' => '',
      'form_element_type' => 'textfield',
      'weight' => 1,
    ],
    'mail_for_notifications' => [
      'title' => t('E-mail address for notifications'),
      'description' => t('E-mail address for notifications of new comments, contact messages and other.'),
      'form_element_type' => 'textfield',
      'weight' => 2,
    ],
    'phone' => [
      'title' => t('Phone number'),
      'form_element_type' => 'textfield',
      'weight' => 3,
    ],
    'company_location' => [
      'title' => t('Company location'),
      'form_element_type' => 'textfield',
      'weight' => 4,
    ],
    'company_schedule' => [
      'title' => t('Work schedule'),
      'form_element_type' => 'textfield',
      'weight' => 5,
    ],
    'personal_data_processing_policy_url' => [
      'title' => t('Personal data processing policy page url'),
      'form_element_type' => 'textfield',
      'weight' => 6,
    ],
  ];
}

/**
 * Return site third-party settings info.
 */
function improvements_get_site_third_party_settings_info(): ?array {
  static $cache;

  if ($cache === NULL) {
    $cache = \Drupal::moduleHandler()->invokeAll('site_third_party_settings_info');
  }

  return $cache;
}

/**
 * Implements hook_form_FORM_ID_alter(): system_site_information_settings.
 *
 * @see \Drupal\system\Form\SiteInformationForm
 */
function improvements_site_information_form_system_site_information_settings_alter(array &$form, FormStateInterface $form_state): void {
  $form['site_information']['site_mail']['#title'] = t('E-mail for "From"');

  $site_third_party_settings_info = improvements_get_site_third_party_settings_info();
  $site_information = improvements_get_site_information();

  foreach ($site_third_party_settings_info as $setting_name => $setting_info) {
    $group = $setting_info['group'] ?? 'site_information';
    $form_element_type = $setting_info['form_element_type'] ?? 'textfield';

    if (!isset($form[$group])) {
      $form[$group] = [
        '#type' => 'details',
        '#open' => TRUE,
      ];

      if (isset($setting_info['group_info'])) {
        $form[$group]['#title'] = $setting_info['group_info']['title'];
      }
    }

    $form[$group][$setting_name] = [
      '#type' => $form_element_type,
      '#title' => $setting_info['title'],
      '#description' => $setting_info['description'] ?? '',
      '#default_value' => $site_information[$setting_name] ?? NULL,
      '#weight' => $setting_info['weight'] ?? 0,
    ];
  }

  $form['#submit'][] = 'improvements_site_information_form_system_site_information_settings_submit';
}

/**
 * Custom submit callback for system_site_information_settings form.
 *
 * @see improvements_form_system_site_information_settings_alter()
 */
function improvements_site_information_form_system_site_information_settings_submit(array &$form, FormStateInterface $form_state): void {
  $site_information_editable_config = \Drupal::configFactory()->getEditable('system.site');
  $site_third_party_settings_info = improvements_get_site_third_party_settings_info();

  foreach ($site_third_party_settings_info as $setting_name => $setting_info) {
    $site_information_editable_config->set('improvements.' . $setting_name, $form_state->getValue($setting_name));
  }

  $site_information_editable_config->save();
}

/**
 * Return site information.
 */
function improvements_get_site_information(string|array $setting_key = NULL): array|string {
  static $site_information;

  if ($site_information === NULL) {
    $site_information = \Drupal::config('system.site')->get();
    if (isset($site_information['improvements'])) {
      $site_information += $site_information['improvements'];
      unset($site_information['improvements']);
    }
  }

  if ($setting_key) {
    if (is_array($setting_key)) {
      return NestedArray::getValue($site_information, $setting_key);
    }
    return $site_information[$setting_key] ?? '';
  }

  return $site_information;
}

/**
 * Implements hook_filter_functions().
 */
function improvements_site_information_filter_functions(): array {
  return [
    'site_information' => [
      'function' => 'function_filter__site_information',
    ],
    'clean_company_phone' => [
      'function' => 'function_filter__clean_company_phone',
    ],
  ];
}

/**
 * Filter function returning site information.
 */
function function_filter__site_information(string $setting_key, int $index = NULL): string {
  if (str_contains($setting_key, '.')) {
    $value = improvements_get_site_information(explode('.', $setting_key));
  }
  else {
    $value = improvements_get_site_information($setting_key);
  }

  if ($index !== NULL) {
    $value = explode('; ', $value);
    return $value[$index] ?? '';
  }

  return $value;
}

/**
 * Filter function returning site information.
 */
function function_filter__clean_company_phone(): string {
  return preg_replace('/\D/', '', improvements_get_site_information('phone'));
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc