3 namespace Drupal\taxonomy\Plugin\views\argument_validator;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\views\Plugin\views\argument_validator\Entity;
10 * Validates whether a term name is a valid term argument.
12 * @ViewsArgumentValidator(
13 * id = "taxonomy_term_name",
14 * title = @Translation("Taxonomy term name"),
15 * entity_type = "taxonomy_term"
18 class TermName extends Entity {
21 * The taxonomy term storage.
23 * @var \Drupal\taxonomy\TermStorageInterface
25 protected $termStorage;
30 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
31 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager);
32 // Not handling exploding term names.
33 $this->multipleCapable = FALSE;
34 $this->termStorage = $entity_manager->getStorage('taxonomy_term');
40 protected function defineOptions() {
41 $options = parent::defineOptions();
42 $options['transform'] = ['default' => FALSE];
50 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
51 parent::buildOptionsForm($form, $form_state);
53 $form['transform'] = [
54 '#type' => 'checkbox',
55 '#title' => $this->t('Transform dashes in URL to spaces in term name filter values'),
56 '#default_value' => $this->options['transform'],
63 public function validateArgument($argument) {
64 if ($this->options['transform']) {
65 $argument = str_replace('-', ' ', $argument);
67 $terms = $this->termStorage->loadByProperties(['name' => $argument]);
70 // Returned empty array no terms with the name.
74 // Not knowing which term will be used if more than one is returned check
76 foreach ($terms as $term) {
77 if (!$this->validateEntity($term)) {