Version 1
[yaffs-website] / web / modules / contrib / entityqueue / src / Plugin / views / filter / EntityQueueInQueue.php
1 <?php
2
3 namespace Drupal\entityqueue\Plugin\views\filter;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\entityqueue\Plugin\views\relationship\EntityQueueRelationship;
7 use Drupal\views\Plugin\views\filter\BooleanOperator;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Filter for entities that are available or not in an entity queue.
12  *
13  * @ingroup views_filter_handlers
14  *
15  * @ViewsFilter("entity_queue_in_queue")
16  */
17 class EntityQueueInQueue extends BooleanOperator {
18
19   /**
20    * The current user.
21    *
22    * @var \Drupal\Core\Session\AccountInterface
23    */
24   protected $currentUser;
25
26   /**
27    * Constructor.
28    *
29    * @param array $configuration
30    *   A configuration array containing information about the plugin instance.
31    * @param string $plugin_id
32    *   The plugin_id for the plugin instance.
33    * @param array $plugin_definition
34    *   The plugin implementation definition.
35    * @param \Drupal\Core\Session\AccountInterface $current_user
36    *   The current user.
37    */
38   public function __construct(array $configuration, $plugin_id, array $plugin_definition, AccountInterface $current_user) {
39     parent::__construct($configuration, $plugin_id, $plugin_definition);
40
41     $this->currentUser = $current_user;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
48     return new static(
49       $configuration,
50       $plugin_id,
51       $plugin_definition,
52       $container->get('current_user')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function query() {
60     $this->ensureMyTable();
61
62     // Try to find an entity queue relationship in this view, and pick the first
63     // one available.
64     $entity_queue_relationship = NULL;
65     foreach ($this->view->relationship as $id => $relationship) {
66       if ($relationship instanceof EntityQueueRelationship) {
67         $entity_queue_relationship = $relationship;
68         $this->options['relationship'] = $id;
69         $this->setRelationship();
70
71         break;
72       }
73     }
74
75     if ($entity_queue_relationship) {
76       $subqueue_items_table_alias = $entity_queue_relationship->first_alias;
77       $field_field = $this->definition['field field'];
78       $operator  = $this->value ? 'IS NOT NULL' : 'IS NULL';
79       $condition = "$subqueue_items_table_alias.$field_field $operator";
80
81       $this->query->addWhereExpression($this->options['group'], $condition);
82     }
83     else {
84       if ($this->currentUser->hasPermission('administer views')) {
85         drupal_set_message($this->t('In order to filter on items from the queue, you need to add the Entityqueue: Queue relationship on View: @view with display: @display', ['@view' => $this->view->storage->label(), '@display' => $this->view->current_display]), 'error');
86       }
87     }
88   }
89
90 }