Drupal 9 Modify A Views Query

[Solved] Drupal 9 Modify A Views Query | Php Frameworks Drupal - Code Explorer | yomemimo.com
Question : drupal 9 modify a views query

Answered by : dave-oukrqevg8554

function hook_views_query_alter(ViewExecutable $view, QueryPluginBase $query) { // (Example assuming a view with an exposed filter on node title.) // If the input for the title filter is a positive integer, filter against // node ID instead of node title. if ($view ->id() == 'my_view' && is_numeric($view->exposed_raw_input['title']) && $view->exposed_raw_input['title'] > 0) { // Traverse through the 'where' part of the query. foreach ($query->where as &$condition_group) { foreach ($condition_group['conditions'] as &$condition) { // If this is the part of the query filtering on title, change the // condition to filter on node ID. if ($condition['field'] == 'node.title') { $condition = [ 'field' => 'node.nid', 'value' => $view->exposed_raw_input['title'], 'operator' => '=', ]; } } } }
}

Source : https://api.drupal.org/api/drupal/core%21modules%21views%21views.api.php/function/hook_views_query_alter/9.0.x | Last Update : Sat, 19 Jun 21

Answers related to drupal 9 modify a views query

Code Explorer Popular Question For Php Frameworks Drupal