Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / views / argument_default / Tid.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\views\argument_default;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\taxonomy\TermInterface;
10 use Drupal\views\ViewExecutable;
11 use Drupal\views\Plugin\views\display\DisplayPluginBase;
12 use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
13 use Drupal\node\NodeInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Drupal\taxonomy\VocabularyStorageInterface;
16
17 /**
18  * Taxonomy tid default argument.
19  *
20  * @ViewsArgumentDefault(
21  *   id = "taxonomy_tid",
22  *   title = @Translation("Taxonomy term ID from URL")
23  * )
24  */
25 class Tid extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
26
27   /**
28    * The route match.
29    *
30    * @var \Drupal\Core\Routing\RouteMatchInterface
31    */
32   protected $routeMatch;
33
34   /**
35    * The vocabulary storage.
36    *
37    * @var \Drupal\taxonomy\VocabularyStorageInterface.
38    */
39   protected $vocabularyStorage;
40
41   /**
42    * Constructs a new Tid instance.
43    *
44    * @param array $configuration
45    *   A configuration array containing information about the plugin instance.
46    * @param string $plugin_id
47    *   The plugin_id for the plugin instance.
48    * @param mixed $plugin_definition
49    *   The plugin implementation definition.   *
50    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
51    *   The route match.
52    * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
53    *   The vocabulary storage.
54    */
55   public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, VocabularyStorageInterface $vocabulary_storage) {
56     parent::__construct($configuration, $plugin_id, $plugin_definition);
57
58     $this->routeMatch = $route_match;
59     $this->vocabularyStorage = $vocabulary_storage;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
66     return new static(
67       $configuration,
68       $plugin_id,
69       $plugin_definition,
70       $container->get('current_route_match'),
71       $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
72     );
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
79     parent::init($view, $display, $options);
80
81     // @todo Remove the legacy code.
82     // Convert legacy vids option to machine name vocabularies.
83     if (!empty($this->options['vids'])) {
84       $vocabularies = taxonomy_vocabulary_get_names();
85       foreach ($this->options['vids'] as $vid) {
86         if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
87           $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
88         }
89       }
90     }
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   protected function defineOptions() {
97     $options = parent::defineOptions();
98
99     $options['term_page'] = ['default' => TRUE];
100     $options['node'] = ['default' => FALSE];
101     $options['anyall'] = ['default' => ','];
102     $options['limit'] = ['default' => FALSE];
103     $options['vids'] = ['default' => []];
104
105     return $options;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
112     $form['term_page'] = [
113       '#type' => 'checkbox',
114       '#title' => $this->t('Load default filter from term page'),
115       '#default_value' => $this->options['term_page'],
116     ];
117     $form['node'] = [
118       '#type' => 'checkbox',
119       '#title' => $this->t("Load default filter from node page, that's good for related taxonomy blocks"),
120       '#default_value' => $this->options['node'],
121     ];
122
123     $form['limit'] = [
124       '#type' => 'checkbox',
125       '#title' => $this->t('Limit terms by vocabulary'),
126       '#default_value' => $this->options['limit'],
127       '#states' => [
128         'visible' => [
129           ':input[name="options[argument_default][taxonomy_tid][node]"]' => ['checked' => TRUE],
130         ],
131       ],
132     ];
133
134     $options = [];
135     $vocabularies = $this->vocabularyStorage->loadMultiple();
136     foreach ($vocabularies as $voc) {
137       $options[$voc->id()] = $voc->label();
138     }
139
140     $form['vids'] = [
141       '#type' => 'checkboxes',
142       '#title' => $this->t('Vocabularies'),
143       '#options' => $options,
144       '#default_value' => $this->options['vids'],
145       '#states' => [
146         'visible' => [
147           ':input[name="options[argument_default][taxonomy_tid][limit]"]' => ['checked' => TRUE],
148           ':input[name="options[argument_default][taxonomy_tid][node]"]' => ['checked' => TRUE],
149         ],
150       ],
151     ];
152
153     $form['anyall'] = [
154       '#type' => 'radios',
155       '#title' => $this->t('Multiple-value handling'),
156       '#default_value' => $this->options['anyall'],
157       '#options' => [
158         ',' => $this->t('Filter to items that share all terms'),
159         '+' => $this->t('Filter to items that share any term'),
160       ],
161       '#states' => [
162         'visible' => [
163           ':input[name="options[argument_default][taxonomy_tid][node]"]' => ['checked' => TRUE],
164         ],
165       ],
166     ];
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function submitOptionsForm(&$form, FormStateInterface $form_state, &$options = []) {
173     // Filter unselected items so we don't unnecessarily store giant arrays.
174     $options['vids'] = array_filter($options['vids']);
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public function getArgument() {
181     // Load default argument from taxonomy page.
182     if (!empty($this->options['term_page'])) {
183       if (($taxonomy_term = $this->routeMatch->getParameter('taxonomy_term')) && $taxonomy_term instanceof TermInterface) {
184         return $taxonomy_term->id();
185       }
186     }
187     // Load default argument from node.
188     if (!empty($this->options['node'])) {
189       // Just check, if a node could be detected.
190       if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) {
191         $taxonomy = [];
192         foreach ($node->getFieldDefinitions() as $field) {
193           if ($field->getType() == 'entity_reference' && $field->getSetting('target_type') == 'taxonomy_term') {
194             $taxonomy_terms = $node->{$field->getName()}->referencedEntities();
195             /** @var \Drupal\taxonomy\TermInterface $taxonomy_term */
196             foreach ($taxonomy_terms as $taxonomy_term) {
197               $taxonomy[$taxonomy_term->id()] = $taxonomy_term->bundle();
198             }
199           }
200         }
201         if (!empty($this->options['limit'])) {
202           $tids = [];
203           // filter by vocabulary
204           foreach ($taxonomy as $tid => $vocab) {
205             if (!empty($this->options['vids'][$vocab])) {
206               $tids[] = $tid;
207             }
208           }
209           return implode($this->options['anyall'], $tids);
210         }
211         // Return all tids.
212         else {
213           return implode($this->options['anyall'], array_keys($taxonomy));
214         }
215       }
216     }
217   }
218
219   /**
220    * {@inheritdoc}
221    */
222   public function getCacheMaxAge() {
223     return Cache::PERMANENT;
224   }
225
226   /**
227    * {@inheritdoc}
228    */
229   public function getCacheContexts() {
230     return ['url'];
231   }
232
233   /**
234    * {@inheritdoc}
235    */
236   public function calculateDependencies() {
237     $dependencies = parent::calculateDependencies();
238
239     foreach ($this->vocabularyStorage->loadMultiple(array_keys($this->options['vids'])) as $vocabulary) {
240       $dependencies[$vocabulary->getConfigDependencyKey()][] = $vocabulary->getConfigDependencyName();
241     }
242     return $dependencies;
243   }
244
245 }