function Term::validate_argument

Overrides ArgumentValidatorPluginBase::validate_argument

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_validator/Term.php, line 91
Definition of Drupal\taxonomy\Plugin\views\argument_validator\Term.

Class

Term
Validate whether an argument is an acceptable node.

Namespace

Drupal\taxonomy\Plugin\views\argument_validator

Code

function validate_argument($argument) {
  $vocabularies = array_filter($this->options['vocabularies']);
  $type = $this->options['type'];
  $transform = $this->options['transform'];
  switch ($type) {
    case 'tid':
      if (!is_numeric($argument)) {
        return FALSE;
      }

      // @todo Deal with missing addTag('term access') that was removed when
      // the db_select that was replaced by the entity_load.
      $term = entity_load('taxonomy_term', $argument);
      if (!$term) {
        return FALSE;
      }
      $this->argument->validated_title = check_plain($term->name);
      return empty($vocabularies) || !empty($vocabularies[$term->vocabulary_machine_name]);
    case 'tids':

      // An empty argument is not a term so doesn't pass.
      if (empty($argument)) {
        return FALSE;
      }
      $tids = new stdClass();
      $tids->value = $argument;
      $tids = $this
        ->breakPhrase($argument, $tids);
      if ($tids->value == array(
        -1,
      )) {
        return FALSE;
      }
      $test = drupal_map_assoc($tids->value);
      $titles = array();

      // check, if some tids already verified
      static $validated_cache = array();
      foreach ($test as $tid) {
        if (isset($validated_cache[$tid])) {
          if ($validated_cache[$tid] === FALSE) {
            return FALSE;
          }
          else {
            $titles[] = $validated_cache[$tid];
            unset($test[$tid]);
          }
        }
      }

      // if unverified tids left - verify them and cache results
      if (count($test)) {
        $result = entity_load_multiple('taxonomy_term', $test);
        foreach ($result as $term) {
          if ($vocabularies && empty($vocabularies[$term->vocabulary_machine_name])) {
            $validated_cache[$term
              ->id()] = FALSE;
            return FALSE;
          }
          $titles[] = $validated_cache[$term
            ->id()] = check_plain($term->name);
          unset($test[$term
            ->id()]);
        }
      }

      // Remove duplicate titles
      $titles = array_unique($titles);
      $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);

      // If this is not empty, we did not find a tid.
      return empty($test);
    case 'name':
    case 'convert':
      $terms = entity_load_multiple_by_properties('taxonomy_term', array(
        'name' => $argument,
      ));
      $term = reset($terms);
      if ($transform) {
        $term->name = str_replace(' ', '-', $term->name);
      }
      if ($term && (empty($vocabularies) || !empty($vocabularies[$term->vocabulary_machine_name]))) {
        if ($type == 'convert') {
          $this->argument->argument = $term
            ->id();
        }
        $this->argument->validated_title = check_plain($term->name);
        return TRUE;
      }
      return FALSE;
  }
}