You are here

tmgmt_smartling_log_settings.module in TMGMT Translator Smartling 8.4

File

modules/tmgmt_smartling_log_settings/tmgmt_smartling_log_settings.module
View source
<?php

/**
 * @file
 */
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;
use Psr\Log\LogLevel;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function tmgmt_smartling_log_settings_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
  $config = \Drupal::configFactory()
    ->getEditable('tmgmt_smartling_log_settings.settings');
  $form['tmgmt_smartling_log_settings_severity_mapping'] = [
    '#type' => 'textarea',
    '#title' => t('Filter out log messages by channel and severity level'),
    '#default_value' => $config
      ->get('severity_mapping'),
    '#attributes' => [
      'placeholder' => '[channel_name]: [severity_level]',
    ],
    '#description' => t('Set severity level for each channel. Valid severity levels are: debug, info, notice, warning, error, critical, alert and emergency. One config per line.'),
  ];
  $form['#validate'][] = 'tmgmt_smartling_log_settings_logging_settings_validate';
  $form['#submit'][] = 'tmgmt_smartling_log_settings_logging_settings_submit';
}

/**
 * Form validation handler for system_logging_settings().
 *
 * @see tmgmt_smartling_log_settings_form_system_logging_settings_alter()
 */
function tmgmt_smartling_log_settings_logging_settings_validate($form, FormStateInterface $form_state) {
  try {
    $config = Yaml::decode($form_state
      ->getValue('tmgmt_smartling_log_settings_severity_mapping'));
    if (!empty($config)) {
      $is_array = is_array($config);
      $all_keys_are_strings = $is_array ? count(array_filter(array_keys($config), 'is_string')) == count($config) : FALSE;
      $all_values_are_valid_severity_levels = $is_array ? count(array_filter(array_values($config), function ($v) {
        return in_array($v, [
          LogLevel::EMERGENCY,
          LogLevel::ALERT,
          LogLevel::CRITICAL,
          LogLevel::ERROR,
          LogLevel::WARNING,
          LogLevel::NOTICE,
          LogLevel::INFO,
          LogLevel::DEBUG,
        ]);
      })) == count($config) : FALSE;
      if (!$is_array || !$all_keys_are_strings || !$all_values_are_valid_severity_levels) {
        $form_state
          ->setErrorByName('tmgmt_smartling_log_settings_severity_mapping', t('Invalid config format.'));
      }
    }
  } catch (Exception $e) {
    $form_state
      ->setErrorByName('tmgmt_smartling_log_settings_severity_mapping', t('Config must be a valid yaml.'));
  }
}

/**
 * Form submission handler for system_logging_settings().
 *
 * @see tmgmt_smartling_log_settings_form_system_logging_settings_alter()
 */
function tmgmt_smartling_log_settings_logging_settings_submit($form, FormStateInterface $form_state) {
  \Drupal::configFactory()
    ->getEditable('tmgmt_smartling_log_settings.settings')
    ->set('severity_mapping', $form_state
    ->getValue('tmgmt_smartling_log_settings_severity_mapping'))
    ->save();
}

Functions

Namesort descending Description
tmgmt_smartling_log_settings_form_system_logging_settings_alter Implements hook_form_FORM_ID_alter().
tmgmt_smartling_log_settings_logging_settings_submit Form submission handler for system_logging_settings().
tmgmt_smartling_log_settings_logging_settings_validate Form validation handler for system_logging_settings().