d09a7e41e4f8b7e90a4deea98e41f752fe366c38
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / hook / views_query_alter.twig
1 /**
2  * Implements hook_views_query_alter().
3  */
4 function {{ machine_name }}_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
5   // (Example assuming a view with an exposed filter on node title.)
6   // If the input for the title filter is a positive integer, filter against
7   // node ID instead of node title.
8   if ($view->id() == 'my_view' && is_numeric($view->exposed_raw_input['title']) && $view->exposed_raw_input['title'] > 0) {
9     // Traverse through the 'where' part of the query.
10     foreach ($query->where as &$condition_group) {
11       foreach ($condition_group['conditions'] as &$condition) {
12         // If this is the part of the query filtering on title, chang the
13         // condition to filter on node ID.
14         if ($condition['field'] == 'node.title') {
15           $condition = [
16             'field' => 'node.nid',
17             'value' => $view->exposed_raw_input['title'],
18             'operator' => '=',
19           ];
20         }
21       }
22     }
23   }
24 }