0

How can I implement hook_views_query_alter() in Drupal 8?

In Drupal 7, I could use the following code.

$args = arg;
$node = menu_get_object();

if ($args[0] === 'my_view') {
  $query->orderBy('node_field_data_created','ASC');
  if (!empty($node) && $node->type == 'category') {
        $filters = $query->getFilters();
        $first_filter = $filters[0];
        if ($first_filter['#value'] == "(category) AND node_field_data.status:(1)") {
            $query->condition('field_category', $node->nid, '=');
        }

In Drupal 8, this code doesn't work.

Can anyone help me out?

2 Answers 2

1

Use it in .module file and change any query as you like

/**
     * Implements hook_views_query_alter().
     */
    function module-name_views_query_alter(\Drupal\views\ViewExecutable $view, $query) {

      if ($view->id() == 'example' && $view->current_display == 'example') {
        if (!empty($view->exposed_raw_input['company_name'])) {
          foreach ($query->where as &$condition_group) {
            foreach ($condition_group['conditions'] as &$condition) {
              if ($condition['field'] == 'node_field_data.type') {
                $condition = array(
                  'field' => 'node_field_data.type',
                  'value' => 'abc',
                  'operator' => '=',
                );
              }
            }
          }
        }
      }
    }
4
  • May you describe what exactly the OP needs to do to change his code?
    – apaderno
    Mar 21, 2017 at 7:46
  • Thank you so much! Could you explain more? For example, what is the company name? How does it sort the node_field_data_created? How to implement the filter?
    – user71452
    Mar 21, 2017 at 22:53
  • Here 'company_name' is one of my field and i am changing the condition of othe field 'node_field_data' with some static value and operator is "="
    – Ajay Reddy
    Mar 22, 2017 at 8:56
  • I tried your code today. I didnt work it out. My purpose is reading menu and fetch the word from menu, then filter the content from this category. For example, 'music' is one of my menu word, then the filter will be 'music'. It will looking the nodes from 'music' category. Anyway, thank you a lot!
    – user71452
    Mar 22, 2017 at 12:04
1

According to the documentation page for hook_views_query_alter(), this hook works in Drupal 8, but (as you can see in comments for the referenced article) you should place it in mymodule.views_execution.inc, not in mymodule.module.

2
  • Those comments aim to D7, they are wrong.
    – DarkteK
    Feb 11, 2021 at 15:41
  • The only implementation of that hook done from Drupal core is placed in a .views_execution.inc file, similarly to the Drupal core implementations of hook_views_query_substitutions(). It doesn't seem the choice of that file is random.
    – apaderno
    Apr 1, 2021 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.