Version 1
[yaffs-website] / web / core / modules / user / src / Plugin / views / filter / Current.php
1 <?php
2
3 namespace Drupal\user\Plugin\views\filter;
4
5 use Drupal\views\Plugin\views\display\DisplayPluginBase;
6 use Drupal\views\ViewExecutable;
7 use Drupal\views\Plugin\views\filter\BooleanOperator;
8
9 /**
10  * Filter handler for the current user.
11  *
12  * @ingroup views_filter_handlers
13  *
14  * @ViewsFilter("user_current")
15  */
16 class Current extends BooleanOperator {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
22     parent::init($view, $display, $options);
23
24     $this->value_value = $this->t('Is the logged in user');
25   }
26
27   public function query() {
28     $this->ensureMyTable();
29
30     $field = $this->tableAlias . '.' . $this->realField . ' ';
31     $or = db_or();
32
33     if (empty($this->value)) {
34       $or->condition($field, '***CURRENT_USER***', '<>');
35       if ($this->accept_null) {
36         $or->isNull($field);
37       }
38     }
39     else {
40       $or->condition($field, '***CURRENT_USER***', '=');
41     }
42     $this->query->addWhere($this->options['group'], $or);
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function getCacheContexts() {
49     $contexts = parent::getCacheContexts();
50
51     // This filter depends on the current user.
52     $contexts[] = 'user';
53
54     return $contexts;
55   }
56
57 }