3 namespace Drupal\entityqueue\Plugin\views\relationship;
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;
14 * A relationship handler for entity queues.
16 * @ingroup views_relationship_handlers
18 * @ViewsRelationship("entity_queue")
20 class EntityQueueRelationship extends EntityReverse implements CacheableDependencyInterface {
25 protected function defineOptions() {
26 $options = parent::defineOptions();
27 $options['limit_queue'] = ['default' => NULL];
34 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
35 $queues = EntityQueue::loadMultipleByTargetType($this->getEntityType());
37 foreach ($queues as $queue) {
38 $options[$queue->id()] = $queue->label();
41 $form['limit_queue'] = [
43 '#title' => $this->t('Limit to a specific entity queue'),
44 '#options' => $options,
45 '#default_value' => $this->options['limit_queue'],
48 parent::buildOptionsForm($form, $form_state);
54 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
55 parent::init($view, $display, $options);
57 // Add an extra condition to limit results based on the queue selection.
58 if ($this->options['limit_queue']) {
59 $this->definition['extra'][] = [
61 'value' => $this->options['limit_queue'],
69 public function calculateDependencies() {
70 $dependencies = parent::calculateDependencies();
72 if ($this->options['limit_queue']) {
73 $queue = EntityQueue::load($this->options['limit_queue']);
74 $dependencies[$queue->getConfigDependencyKey()][] = $queue->getConfigDependencyName();
83 public function getCacheContexts() {
90 public function getCacheTags() {
93 if ($this->options['limit_queue']) {
94 $queue = EntityQueue::load($this->options['limit_queue']);
95 $tags = $queue->getCacheTags();
104 public function getCacheMaxAge() {
105 return Cache::PERMANENT;
111 public function query() {
112 // Add a 'where' condition if needed.
113 if (!empty($this->definition['extra'])) {
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'];
122 if (count($bundles) > 0) {
123 $this->definition['join_extra'][] = [
130 // Now - let's build the query.