You are here

function TermReference::validateTermStrings in EntityFieldQuery Views Backend 8

Validate the user string. Since this can come from either the form or the exposed filter, this is abstracted out a bit so it can handle the multiple input sources.

Parameters

$form: The form which is used, either the views ui or the exposed filters.

$values: The taxonomy names which will be converted to tids.

Return value

array The taxonomy ids fo all validated terms.

2 calls to TermReference::validateTermStrings()
TermReference::validateExposed in src/Plugin/views/filter/TermReference.php
Validate the exposed handler form
TermReference::valueValidate in src/Plugin/views/filter/TermReference.php
Validate the options form.

File

src/Plugin/views/filter/TermReference.php, line 285
Contains \Drupal\efq_views\Plugin\views\filter\FieldInOperator.

Class

TermReference
Filter by term id.

Namespace

Drupal\efq_views\Plugin\views\filter

Code

function validateTermStrings(&$form, $values) {
  if (empty($values)) {
    return array();
  }
  $vocabulary = taxonomy_vocabulary_machine_name_load($this->options['vocabulary']);
  $tids = array();
  $names = array();
  $missing = array();
  foreach ($values as $value) {
    $missing[strtolower($value)] = TRUE;
    $names[] = $value;
  }
  if (!$names) {
    return FALSE;
  }
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'taxonomy_term')
    ->propertyCondition('vid', (int) $vocabulary->vid)
    ->propertyCondition('name', $names)
    ->execute();
  if (!empty($result['taxonomy_term'])) {
    $terms = entity_load('taxonomy_term', array_keys($result['taxonomy_term']));
    foreach ($terms as $term) {
      unset($missing[strtolower($term->name)]);
      $tids[] = $term->tid;
    }
  }
  if ($missing && !empty($this->options['error_message'])) {
    form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array(
      '@terms' => implode(', ', array_keys($missing)),
    )));
  }
  elseif ($missing && empty($this->options['error_message'])) {
    $tids = array(
      0,
    );
  }
  return $tids;
}