You are here

function element_node_search_config_groupings_validate in Search configuration 7

Same name and namespace in other branches
  1. 8 search_config.module \element_node_search_config_groupings_validate()

Element validate callback for groupings list.

User friendly display of key|value listings element validation.

1 string reference to 'element_node_search_config_groupings_validate'
_search_config_form_search_admin_settings_alter in ./search_config.admin.inc
Called from search_config_form_search_admin_settings_alter().

File

./search_config.admin.inc, line 323
Provides the search administration form for search_config.

Code

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

  // Pre-tidy the options list.
  $list = array_filter(array_map('trim', $list), 'strlen');
  $values = array();
  $content_types = search_config_content_types();
  $found_types = array();
  $errors = array();
  $empty_pairs = array();
  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[] = check_plain($matches[0]);
      }
      else {
        $found_types = array();
        $unknown_types = array();
        $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]', array(
            '%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]', array(
            '%line' => $matches[0],
          ));
        }
      }
    }
    else {
      $empty_pairs[] = check_plain($text);
    }
  }
  if (!empty($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', array(
      '!list' => theme('item_list', array(
        'items' => $empty_pairs,
      )),
    ));
  }
  if (!empty($errors)) {
    form_error($element, t('The following errors were detected in the group options: !list', array(
      '!list' => theme('item_list', array(
        'items' => $errors,
      )),
    )));
  }
  else {
    form_set_value($element, $values, $form_state);
  }
}