86eb1f3361bae7edee38fbe98064c9c9447e0ef9
[yaffs-website] / web / core / modules / views / src / Plugin / views / filter / ManyToOne.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\filter;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewExecutable;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\ManyToOneHelper;
9
10 /**
11  * Complex filter to handle filtering for many to one relationships,
12  * such as terms (many terms per node) or roles (many roles per user).
13  *
14  * The construct method needs to be overridden to provide a list of options;
15  * alternately, the valueForm and adminSummary methods need to be overridden
16  * to provide something that isn't just a select list.
17  *
18  * @ingroup views_filter_handlers
19  *
20  * @ViewsFilter("many_to_one")
21  */
22 class ManyToOne extends InOperator {
23
24   /**
25    * @var \Drupal\views\ManyToOneHelper
26    *
27    * Stores the Helper object which handles the many_to_one complexity.
28    */
29   public $helper = NULL;
30
31   /**
32    * {@inheritdoc}
33    */
34   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
35     parent::init($view, $display, $options);
36
37     $this->helper = new ManyToOneHelper($this);
38   }
39
40   protected function defineOptions() {
41     $options = parent::defineOptions();
42
43     $options['operator']['default'] = 'or';
44     $options['value']['default'] = [];
45
46     if (isset($this->helper)) {
47       $this->helper->defineOptions($options);
48     }
49     else {
50       $helper = new ManyToOneHelper($this);
51       $helper->defineOptions($options);
52     }
53
54     return $options;
55   }
56
57   public function operators() {
58     $operators = [
59       'or' => [
60         'title' => $this->t('Is one of'),
61         'short' => $this->t('or'),
62         'short_single' => $this->t('='),
63         'method' => 'opHelper',
64         'values' => 1,
65         'ensure_my_table' => 'helper',
66       ],
67       'and' => [
68         'title' => $this->t('Is all of'),
69         'short' => $this->t('and'),
70         'short_single' => $this->t('='),
71         'method' => 'opHelper',
72         'values' => 1,
73         'ensure_my_table' => 'helper',
74       ],
75       'not' => [
76         'title' => $this->t('Is none of'),
77         'short' => $this->t('not'),
78         'short_single' => $this->t('<>'),
79         'method' => 'opHelper',
80         'values' => 1,
81         'ensure_my_table' => 'helper',
82       ],
83     ];
84     // if the definition allows for the empty operator, add it.
85     if (!empty($this->definition['allow empty'])) {
86       $operators += [
87         'empty' => [
88           'title' => $this->t('Is empty (NULL)'),
89           'method' => 'opEmpty',
90           'short' => $this->t('empty'),
91           'values' => 0,
92         ],
93         'not empty' => [
94           'title' => $this->t('Is not empty (NOT NULL)'),
95           'method' => 'opEmpty',
96           'short' => $this->t('not empty'),
97           'values' => 0,
98         ],
99       ];
100     }
101
102     return $operators;
103   }
104
105   protected $valueFormType = 'select';
106   protected function valueForm(&$form, FormStateInterface $form_state) {
107     parent::valueForm($form, $form_state);
108
109     if (!$form_state->get('exposed')) {
110       $this->helper->buildOptionsForm($form, $form_state);
111     }
112   }
113
114   /**
115    * Override ensureMyTable so we can control how this joins in.
116    * The operator actually has influence over joining.
117    */
118   public function ensureMyTable() {
119     // Defer to helper if the operator specifies it.
120     $info = $this->operators();
121     if (isset($info[$this->operator]['ensure_my_table']) && $info[$this->operator]['ensure_my_table'] == 'helper') {
122       return $this->helper->ensureMyTable();
123     }
124
125     return parent::ensureMyTable();
126   }
127
128   protected function opHelper() {
129     if (empty($this->value)) {
130       return;
131     }
132     // Form API returns unchecked options in the form of option_id => 0. This
133     // breaks the generated query for "is all of" filters so we remove them.
134     $this->value = array_filter($this->value, 'static::arrayFilterZero');
135     $this->helper->addFilter();
136   }
137
138 }