You are here

function element_node_search_config_groupings_validate in Search configuration 8

Same name and namespace in other branches
  1. 7 search_config.admin.inc \element_node_search_config_groupings_validate()

Element validate callback for groupings list.

User friendly display of key|value listings element validation.

Parameters

$element:

FormStateInterface $form_state:

1 string reference to 'element_node_search_config_groupings_validate'
search_config_form_search_admin_settings_alter in ./search_config.module
Implements hook_form_FORM_alter()

File

./search_config.module, line 519
The module that search form, including enforcing search restrictions by content type.

Code

function element_node_search_config_groupings_validate($element, FormStateInterface &$form_state) {
  $list = explode("\n", $element['#value']);

  // Pre-tidy the options list.
  $list = array_filter(array_map('trim', $list), 'strlen');
  $values = [];
  $content_types = search_config_content_types();
  $found_types = [];
  $errors = [];
  $empty_pairs = [];
  foreach ($list as $text) {
    if (preg_match('/([a-z0-9_,\\-<>]{1,})\\|(.{1,})/', $text, $matches)) {
      $key = trim($matches[1]);
      $value = trim($matches[2]);
      if (empty($key) || empty($value)) {
        $empty_pairs[] = Html::escape($matches[0]);
      }
      else {
        $found_types = [];
        $unknown_types = [];
        $types = array_filter(array_map('trim', explode(',', $key)), 'strlen');
        foreach ($types as $name) {
          if (isset($content_types[$name]) || $name == '<other-types>' || $name == '<all-types>') {
            $found_types[] = $name;
          }
          else {
            $unknown_types[] = $name;
          }
        }
        if (count($unknown_types)) {
          $errors[] = t('The key contains one or more invalid content types: %types [line: %line]', [
            '%types' => implode(', ', $unknown_types),
            '%line' => $matches[0],
          ]);
        }
        elseif (count($found_types)) {
          $values[implode(',', $found_types)] = $value;
        }
        else {
          $errors[] = t('No types could be found. [line: %line]', [
            '%line' => $matches[0],
          ]);
        }
      }
    }
    else {
      $empty_pairs[] = Html::escape($text);
    }
  }
  if (!empty($empty_pairs)) {
    $item_list = [
      '#type' => 'item_list',
      '#items' => $empty_pairs,
    ];
    $errors[] = t('Each line must contain a "type|value" pair. Types must contain one or more content type machine codes separated by commas and values must be non-empty strings. This error was seen on the following lines: !list', [
      '!list' => \Drupal::service('renderer')
        ->render($item_list),
    ]);
  }
  if (!empty($errors)) {
    $item_list = [
      '#type' => 'item_list',
      '#items' => $errors,
    ];
    $form_state
      ->setErrorByName($element, t('The following errors were detected in the group options: !list', [
      '!list' => \Drupal::service('renderer')
        ->render($item_list),
    ]));
  }
  else {
    $form_state
      ->setErrorByName($element, $values);
  }
}