3 namespace Drupal\views;
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\HandlerBase;
10 * This many to one helper object is used on both arguments and filters.
12 * @todo This requires extensive documentation on how this class is to
13 * be used. For now, look at the arguments and filters that use it. Lots
14 * of stuff is just pass-through but there are definitely some interesting
15 * areas where they interact.
17 * Any handler that uses this can have the following possibly additional
19 * - numeric: If true, treat this field as numeric, using %d instead of %s in
22 class ManyToOneHelper {
24 public function __construct($handler) {
25 $this->handler = $handler;
28 public static function defineOptions(&$options) {
29 $options['reduce_duplicates'] = ['default' => FALSE];
32 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
33 $form['reduce_duplicates'] = [
34 '#type' => 'checkbox',
35 '#title' => t('Reduce duplicates'),
36 '#description' => t("This filter can cause items that have more than one of the selected options to appear as duplicate results. If this filter causes duplicate results to occur, this checkbox can reduce those duplicates; however, the more terms it has to search for, the less performant the query will be, so use this with caution. Shouldn't be set on single-value fields, as it may cause values to disappear from display, if used on an incompatible field."),
37 '#default_value' => !empty($this->handler->options['reduce_duplicates']),
43 * Sometimes the handler might want us to use some kind of formula, so give
44 * it that option. If it wants us to do this, it must set $helper->formula = TRUE
45 * and implement handler->getFormula();
47 public function getField() {
48 if (!empty($this->formula)) {
49 return $this->handler->getFormula();
52 return $this->handler->tableAlias . '.' . $this->handler->realField;
57 * Add a table to the query.
59 * This is an advanced concept; not only does it add a new instance of the table,
60 * but it follows the relationship path all the way down to the relationship
61 * link point and adds *that* as a new relationship and then adds the table to
62 * the relationship, if necessary.
64 public function addTable($join = NULL, $alias = NULL) {
65 // This is used for lookups in the many_to_one table.
66 $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
69 $join = $this->getJoin();
72 // See if there's a chain between us and the base relationship. If so, we need
73 // to create a new relationship to use.
74 $relationship = $this->handler->relationship;
76 // Determine the primary table to seek
77 if (empty($this->handler->query->relationships[$relationship])) {
78 $base_table = $this->handler->view->storage->get('base_table');
81 $base_table = $this->handler->query->relationships[$relationship]['base'];
84 // Cycle through the joins. This isn't as error-safe as the normal
85 // ensurePath logic. Perhaps it should be.
86 $r_join = clone $join;
87 while ($r_join->leftTable != $base_table) {
88 $r_join = HandlerBase::getTableJoin($r_join->leftTable, $base_table);
90 // If we found that there are tables in between, add the relationship.
91 if ($r_join->table != $join->table) {
92 $relationship = $this->handler->query->addRelationship($this->handler->table . '_' . $r_join->table, $r_join, $r_join->table, $this->handler->relationship);
95 // And now add our table, using the new relationship if one was used.
96 $alias = $this->handler->query->addTable($this->handler->table, $relationship, $join, $alias);
98 // Store what values are used by this table chain so that other chains can
99 // automatically discard those values.
100 if (empty($this->handler->view->many_to_one_tables[$field])) {
101 $this->handler->view->many_to_one_tables[$field] = $this->handler->value;
104 $this->handler->view->many_to_one_tables[$field] = array_merge($this->handler->view->many_to_one_tables[$field], $this->handler->value);
110 public function getJoin() {
111 return $this->handler->getJoin();
115 * Provide the proper join for summary queries. This is important in part because
116 * it will cooperate with other arguments if possible.
118 public function summaryJoin() {
119 $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
120 $join = $this->getJoin();
123 $options = $this->handler->options;
124 $view = $this->handler->view;
125 $query = $this->handler->query;
127 if (!empty($options['require_value'])) {
128 $join->type = 'INNER';
131 if (empty($options['add_table']) || empty($view->many_to_one_tables[$field])) {
132 return $query->ensureTable($this->handler->table, $this->handler->relationship, $join);
135 if (!empty($view->many_to_one_tables[$field])) {
136 foreach ($view->many_to_one_tables[$field] as $value) {
139 'field' => $this->handler->realField,
142 'numeric' => !empty($this->definition['numeric']),
147 return $this->addTable($join);
152 * Override ensureMyTable so we can control how this joins in.
153 * The operator actually has influence over joining.
155 public function ensureMyTable() {
156 if (!isset($this->handler->tableAlias)) {
157 // Case 1: Operator is an 'or' and we're not reducing duplicates.
158 // We hence get the absolute simplest:
159 $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
160 if ($this->handler->operator == 'or' && empty($this->handler->options['reduce_duplicates'])) {
161 if (empty($this->handler->options['add_table']) && empty($this->handler->view->many_to_one_tables[$field])) {
162 // query optimization, INNER joins are slightly faster, so use them
163 // when we know we can.
164 $join = $this->getJoin();
166 $join->type = 'INNER';
168 $this->handler->tableAlias = $this->handler->query->ensureTable($this->handler->table, $this->handler->relationship, $join);
169 $this->handler->view->many_to_one_tables[$field] = $this->handler->value;
172 $join = $this->getJoin();
173 $join->type = 'LEFT';
174 if (!empty($this->handler->view->many_to_one_tables[$field])) {
175 foreach ($this->handler->view->many_to_one_tables[$field] as $value) {
178 'field' => $this->handler->realField,
181 'numeric' => !empty($this->handler->definition['numeric']),
187 $this->handler->tableAlias = $this->addTable($join);
190 return $this->handler->tableAlias;
193 // Case 2: it's an 'and' or an 'or'.
194 // We do one join per selected value.
195 if ($this->handler->operator != 'not') {
196 // Clone the join for each table:
197 $this->handler->tableAliases = [];
198 foreach ($this->handler->value as $value) {
199 $join = $this->getJoin();
200 if ($this->handler->operator == 'and') {
201 $join->type = 'INNER';
205 'field' => $this->handler->realField,
207 'numeric' => !empty($this->handler->definition['numeric']),
211 // The table alias needs to be unique to this value across the
212 // multiple times the filter or argument is called by the view.
213 if (!isset($this->handler->view->many_to_one_aliases[$field][$value])) {
214 if (!isset($this->handler->view->many_to_one_count[$this->handler->table])) {
215 $this->handler->view->many_to_one_count[$this->handler->table] = 0;
217 $this->handler->view->many_to_one_aliases[$field][$value] = $this->handler->table . '_value_' . ($this->handler->view->many_to_one_count[$this->handler->table]++);
220 $this->handler->tableAliases[$value] = $this->addTable($join, $this->handler->view->many_to_one_aliases[$field][$value]);
221 // Set tableAlias to the first of these.
222 if (empty($this->handler->tableAlias)) {
223 $this->handler->tableAlias = $this->handler->tableAliases[$value];
227 // Case 3: it's a 'not'.
228 // We just do one join. We'll add a where clause during
229 // the query phase to ensure that $table.$field IS NULL.
231 $join = $this->getJoin();
232 $join->type = 'LEFT';
234 $join->extraOperator = 'OR';
235 foreach ($this->handler->value as $value) {
237 'field' => $this->handler->realField,
239 'numeric' => !empty($this->handler->definition['numeric']),
243 $this->handler->tableAlias = $this->addTable($join);
246 return $this->handler->tableAlias;
250 * Provides a unique placeholders for handlers.
252 protected function placeholder() {
253 return $this->handler->query->placeholder($this->handler->options['table'] . '_' . $this->handler->options['field']);
256 public function addFilter() {
257 if (empty($this->handler->value)) {
260 $this->handler->ensureMyTable();
262 // Shorten some variables:
263 $field = $this->getField();
264 $options = $this->handler->options;
265 $operator = $this->handler->operator;
266 $formula = !empty($this->formula);
267 $value = $this->handler->value;
268 if (empty($options['group'])) {
269 $options['group'] = 0;
272 // If $add_condition is set to FALSE, a single expression is enough. If it
273 // is set to TRUE, conditions will be added.
274 $add_condition = TRUE;
275 if ($operator == 'not') {
277 $operator = 'IS NULL';
278 $add_condition = FALSE;
280 elseif ($operator == 'or' && empty($options['reduce_duplicates'])) {
281 if (count($value) > 1) {
285 $value = is_array($value) ? array_pop($value) : $value;
288 $add_condition = FALSE;
291 if (!$add_condition) {
293 $placeholder = $this->placeholder();
294 if ($operator == 'IN') {
295 $operator = "$operator IN($placeholder)";
298 $operator = "$operator $placeholder";
301 $placeholder => $value,
303 $this->handler->query->addWhereExpression($options['group'], "$field $operator", $placeholders);
306 $placeholder = $this->placeholder();
307 if (count($this->handler->value) > 1) {
308 $placeholder .= '[]';
310 if ($operator == 'IS NULL') {
311 $this->handler->query->addWhereExpression(0, "$field $operator");
314 $this->handler->query->addWhereExpression(0, "$field $operator($placeholder)", [$placeholder => $value]);
318 if ($operator == 'IS NULL') {
319 $this->handler->query->addWhereExpression(0, "$field $operator");
322 $this->handler->query->addWhereExpression(0, "$field $operator $placeholder", [$placeholder => $value]);
328 if ($add_condition) {
329 $field = $this->handler->realField;
330 $clause = $operator == 'or' ? new Condition('OR') : new Condition('AND');
331 foreach ($this->handler->tableAliases as $value => $alias) {
332 $clause->condition("$alias.$field", $value);
335 // implode on either AND or OR.
336 $this->handler->query->addWhere($options['group'], $clause);