You are here

function tmgmt_smartling_log_settings_logging_settings_validate in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 modules/tmgmt_smartling_log_settings/tmgmt_smartling_log_settings.module \tmgmt_smartling_log_settings_logging_settings_validate()

Form validation handler for system_logging_settings().

See also

tmgmt_smartling_log_settings_form_system_logging_settings_alter()

1 string reference to 'tmgmt_smartling_log_settings_logging_settings_validate'
tmgmt_smartling_log_settings_form_system_logging_settings_alter in modules/tmgmt_smartling_log_settings/tmgmt_smartling_log_settings.module
Implements hook_form_FORM_ID_alter().

File

modules/tmgmt_smartling_log_settings/tmgmt_smartling_log_settings.module, line 35

Code

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.'));
  }
}