3 namespace Drupal\taxonomy\Plugin\views\argument_default;
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;
18 * Taxonomy tid default argument.
20 * @ViewsArgumentDefault(
21 * id = "taxonomy_tid",
22 * title = @Translation("Taxonomy term ID from URL")
25 class Tid extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
30 * @var \Drupal\Core\Routing\RouteMatchInterface
32 protected $routeMatch;
35 * The vocabulary storage.
37 * @var \Drupal\taxonomy\VocabularyStorageInterface.
39 protected $vocabularyStorage;
42 * Constructs a new Tid instance.
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
52 * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
53 * The vocabulary storage.
55 public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, VocabularyStorageInterface $vocabulary_storage) {
56 parent::__construct($configuration, $plugin_id, $plugin_definition);
58 $this->routeMatch = $route_match;
59 $this->vocabularyStorage = $vocabulary_storage;
65 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
70 $container->get('current_route_match'),
71 $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
78 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
79 parent::init($view, $display, $options);
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;
96 protected function defineOptions() {
97 $options = parent::defineOptions();
99 $options['term_page'] = ['default' => TRUE];
100 $options['node'] = ['default' => FALSE];
101 $options['anyall'] = ['default' => ','];
102 $options['limit'] = ['default' => FALSE];
103 $options['vids'] = ['default' => []];
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'],
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'],
124 '#type' => 'checkbox',
125 '#title' => $this->t('Limit terms by vocabulary'),
126 '#default_value' => $this->options['limit'],
129 ':input[name="options[argument_default][taxonomy_tid][node]"]' => ['checked' => TRUE],
135 $vocabularies = $this->vocabularyStorage->loadMultiple();
136 foreach ($vocabularies as $voc) {
137 $options[$voc->id()] = $voc->label();
141 '#type' => 'checkboxes',
142 '#title' => $this->t('Vocabularies'),
143 '#options' => $options,
144 '#default_value' => $this->options['vids'],
147 ':input[name="options[argument_default][taxonomy_tid][limit]"]' => ['checked' => TRUE],
148 ':input[name="options[argument_default][taxonomy_tid][node]"]' => ['checked' => TRUE],
155 '#title' => $this->t('Multiple-value handling'),
156 '#default_value' => $this->options['anyall'],
158 ',' => $this->t('Filter to items that share all terms'),
159 '+' => $this->t('Filter to items that share any term'),
163 ':input[name="options[argument_default][taxonomy_tid][node]"]' => ['checked' => TRUE],
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']);
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();
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) {
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();
201 if (!empty($this->options['limit'])) {
203 // filter by vocabulary
204 foreach ($taxonomy as $tid => $vocab) {
205 if (!empty($this->options['vids'][$vocab])) {
209 return implode($this->options['anyall'], $tids);
213 return implode($this->options['anyall'], array_keys($taxonomy));
222 public function getCacheMaxAge() {
223 return Cache::PERMANENT;
229 public function getCacheContexts() {
236 public function calculateDependencies() {
237 $dependencies = parent::calculateDependencies();
239 foreach ($this->vocabularyStorage->loadMultiple(array_keys($this->options['vids'])) as $vocabulary) {
240 $dependencies[$vocabulary->getConfigDependencyKey()][] = $vocabulary->getConfigDependencyName();
242 return $dependencies;