TermName.php

Same filename in this branch
  1. 11.x core/modules/taxonomy/src/Plugin/views/field/TermName.php
Same filename in other branches
  1. 9 core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php
  2. 9 core/modules/taxonomy/src/Plugin/views/field/TermName.php
  3. 8.9.x core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php
  4. 8.9.x core/modules/taxonomy/src/Plugin/views/field/TermName.php
  5. 10 core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php
  6. 10 core/modules/taxonomy/src/Plugin/views/field/TermName.php

Namespace

Drupal\taxonomy\Plugin\views\argument_validator

File

core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php

View source
<?php

namespace Drupal\taxonomy\Plugin\views\argument_validator;

use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Attribute\ViewsArgumentValidator;
use Drupal\views\Plugin\views\argument_validator\Entity;

/**
 * Validates whether a term name is a valid term argument.
 */
class TermName extends Entity {
    
    /**
     * The taxonomy term storage.
     *
     * @var \Drupal\taxonomy\TermStorageInterface
     */
    protected $termStorage;
    
    /**
     * {@inheritdoc}
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) {
        parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_type_bundle_info);
        // Not handling exploding term names.
        $this->multipleCapable = FALSE;
        $this->termStorage = $entity_type_manager->getStorage('taxonomy_term');
    }
    
    /**
     * {@inheritdoc}
     */
    protected function defineOptions() {
        $options = parent::defineOptions();
        $options['transform'] = [
            'default' => FALSE,
        ];
        return $options;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildOptionsForm(&$form, FormStateInterface $form_state) {
        parent::buildOptionsForm($form, $form_state);
        $form['transform'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Transform dashes in URL to spaces in term name filter values'),
            '#default_value' => $this->options['transform'],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateArgument($argument) {
        if ($this->options['transform']) {
            $argument = str_replace('-', ' ', $argument);
            $this->argument->argument = $argument;
        }
        // If bundles is set then restrict the loaded terms to the given bundles.
        if (!empty($this->options['bundles'])) {
            $terms = $this->termStorage
                ->loadByProperties([
                'name' => $argument,
                'vid' => $this->options['bundles'],
            ]);
        }
        else {
            $terms = $this->termStorage
                ->loadByProperties([
                'name' => $argument,
            ]);
        }
        // $terms are already bundle tested but we need to test access control.
        foreach ($terms as $term) {
            if ($this->validateEntity($term)) {
                return TRUE;
            }
        }
        return FALSE;
    }

}

Classes

Title Deprecated Summary
TermName Validates whether a term name is a valid term argument.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.