Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / views / argument_validator / TermName.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\views\argument_validator;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\views\Plugin\views\argument_validator\Entity;
8
9 /**
10  * Validates whether a term name is a valid term argument.
11  *
12  * @ViewsArgumentValidator(
13  *   id = "taxonomy_term_name",
14  *   title = @Translation("Taxonomy term name"),
15  *   entity_type = "taxonomy_term"
16  * )
17  */
18 class TermName extends Entity {
19
20   /**
21    * The taxonomy term storage.
22    *
23    * @var \Drupal\taxonomy\TermStorageInterface
24    */
25   protected $termStorage;
26
27   /**
28    * {@inheritdoc}
29    */
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');
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function defineOptions() {
41     $options = parent::defineOptions();
42     $options['transform'] = ['default' => FALSE];
43
44     return $options;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
51     parent::buildOptionsForm($form, $form_state);
52
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'],
57     ];
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function validateArgument($argument) {
64     if ($this->options['transform']) {
65       $argument = str_replace('-', ' ', $argument);
66     }
67     $terms = $this->termStorage->loadByProperties(['name' => $argument]);
68
69     if (!$terms) {
70       // Returned empty array no terms with the name.
71       return FALSE;
72     }
73
74     // Not knowing which term will be used if more than one is returned check
75     // each one.
76     foreach ($terms as $term) {
77       if (!$this->validateEntity($term)) {
78         return FALSE;
79       }
80     }
81
82     return TRUE;
83   }
84
85 }