You are here

public function TermMachineName::validateArgument in Taxonomy Machine Name 8

Performs validation for a given argument.

Overrides Entity::validateArgument

File

src/Plugin/views/argument_validator/TermMachineName.php, line 64

Class

TermMachineName
Validates whether a term machine name is a valid term argument.

Namespace

Drupal\taxonomy_machine_name\Plugin\views\argument_validator

Code

public function validateArgument($argument) {
  if ($this->options['transform']) {
    $argument = str_replace('-', '_', $argument);
  }
  $properties = [
    'machine_name' => $argument,
  ];
  if ($bundles = array_filter($this->options['bundles'])) {
    $properties['vid'] = $bundles;
  }
  $terms = $this->termStorage
    ->loadByProperties($properties);
  if (!$terms) {

    // Returned empty array no terms with the name.
    return FALSE;
  }

  // Not knowing which term will be used if more than one is returned check
  // each one.

  /** @var \Drupal\taxonomy\Entity\Term $term */
  foreach ($terms as $term) {
    if (!$this
      ->validateEntity($term)) {
      return FALSE;
    }
  }
  $this->argument->argument = $term
    ->id();

  // Property created dynamically.
  if (!($this->argument->validated_title = $term
    ->getName())) {
    $this->argument->validated_title = $this
      ->t('No name');
  }
  return TRUE;
}