Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entityqueue / src / Plugin / views / relationship / EntityQueueRelationship.php
1 <?php
2
3 namespace Drupal\entityqueue\Plugin\views\relationship;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\entityqueue\Entity\EntityQueue;
9 use Drupal\views\Plugin\views\display\DisplayPluginBase;
10 use Drupal\views\Plugin\views\relationship\EntityReverse;
11 use Drupal\views\ViewExecutable;
12
13 /**
14  * A relationship handler for entity queues.
15  *
16  * @ingroup views_relationship_handlers
17  *
18  * @ViewsRelationship("entity_queue")
19  */
20 class EntityQueueRelationship extends EntityReverse implements CacheableDependencyInterface {
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function defineOptions() {
26     $options = parent::defineOptions();
27     $options['limit_queue'] = ['default' => NULL];
28     return $options;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
35     $queues = EntityQueue::loadMultipleByTargetType($this->getEntityType());
36     $options = [];
37     foreach ($queues as $queue) {
38       $options[$queue->id()] = $queue->label();
39     }
40
41     $form['limit_queue'] = [
42       '#type' => 'radios',
43       '#title' => $this->t('Limit to a specific entity queue'),
44       '#options' => $options,
45       '#default_value' => $this->options['limit_queue'],
46     ];
47
48     parent::buildOptionsForm($form, $form_state);
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
55     parent::init($view, $display, $options);
56
57     // Add an extra condition to limit results based on the queue selection.
58     if ($this->options['limit_queue']) {
59       $this->definition['extra'][] = [
60         'field' => 'bundle',
61         'value' => $this->options['limit_queue'],
62       ];
63     }
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function calculateDependencies() {
70     $dependencies = parent::calculateDependencies();
71
72     if ($this->options['limit_queue']) {
73       $queue = EntityQueue::load($this->options['limit_queue']);
74       $dependencies[$queue->getConfigDependencyKey()][] = $queue->getConfigDependencyName();
75     }
76
77     return $dependencies;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function getCacheContexts() {
84     return [];
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function getCacheTags() {
91     $tags = [];
92
93     if ($this->options['limit_queue']) {
94       $queue = EntityQueue::load($this->options['limit_queue']);
95       $tags = $queue->getCacheTags();
96     }
97
98     return $tags;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function getCacheMaxAge() {
105     return Cache::PERMANENT;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function query() {
112     // Add a 'where' condition if needed.
113     if (!empty($this->definition['extra'])) {
114       $bundles = [];
115
116       // Future-proofing: support any number of selected bundles.
117       foreach ($this->definition['extra'] as $extra) {
118         if ($extra['field'] == 'bundle') {
119           $bundles[] = $extra['value'];
120         }
121       }
122       if (count($bundles) > 0) {
123         $this->definition['join_extra'][] = [
124           'field' => 'bundle',
125           'value' => $bundles,
126         ];
127       }
128     }
129
130     // Now - let's build the query.
131     parent::query();
132   }
133
134 }