Skip to content

Commit 8acdd4f

Browse files
Merge PR #5 from official repository
1 parent cc7ee2e commit 8acdd4f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* View execution hooks for views_selective_filters.
6+
*/
7+
8+
use Drupal\views\ViewExecutable;
9+
use Drupal\views\Plugin\views\query\QueryPluginBase;
10+
11+
/**
12+
* Implements hook_views_query_alter().
13+
*
14+
* Since this filter plugin screws up entity reference fields, find any filter
15+
* submission that ends with _target_id_selective and fix query conditions.
16+
*
17+
* From: 'node__field_some_entity_reference.field_some_entity_reference'
18+
* To: 'node__field_some_entity_reference.field_some_entity_reference_target_id'
19+
*/
20+
function views_selective_filters_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
21+
$field_suffix = '_selective';
22+
$missing = '_target_id';
23+
$uses_selective_filters = FALSE;
24+
$replacement_map = [];
25+
foreach ($view->filter as $filter_name => $filter) {
26+
if (preg_match("/${missing}${field_suffix}$/", $filter_name) === 1) {
27+
$uses_selective_filters = TRUE;
28+
$replace_with = preg_replace("/${field_suffix}$/", '', $filter_name);
29+
$look_for = preg_replace("/${missing}$/", '', $replace_with);
30+
$replacement_map[$look_for] = $replace_with;
31+
}
32+
}
33+
34+
if ($uses_selective_filters) {
35+
foreach ($query->where as &$condition_group) {
36+
foreach ($condition_group['conditions'] as &$condition) {
37+
$exploded = explode('.', $condition['field']);
38+
$search_key = array_pop($exploded);
39+
if (isset($replacement_map[$search_key])) {
40+
$condition['field'] = preg_replace("/.${search_key}$/", '.' . $replacement_map[$search_key], $condition['field']);
41+
}
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)