Drupal - How do I implement hook_views_query_alter()?

In Drupal 8, best practice is to use hook_views_query_alter() in MYMODULE.views_execution.inc file. If instead it is placed in MYMODULE.views.inc, it will be executed only when rebuilding the views cache, causing your view to function properly once and then not subsequently.

MYMODULE.views_execution.inc file should have this structure...

use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;

/**
 * Implements hook_views_query_alter().
 */
function MYMODULE_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {

  // Only for my_view view.
  if ($view->id() == 'my_view') {
    // some action
  }

}

Ref: views_test_data.views_execution.inc


Hooks need to be implemented in the .module file.

Just move the hook to your .module file, rebuild the cache, and Drupal will pick it up.

You could of course require a file that contains your hooks, as long as when that file is parsed, the hook functions are available in it.

You can read a bit more about hooks in the docs.


creating hook_views_query_alter in d8 is not same as d7, ofcourse.. after many try, i managed to get this one.. hope this helps.. its for different GET parameter ..

function MYMODULE_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
if ( ($view->id() ==  'MY_VIEW' && $view->current_display == 'page_1')) {
  $allowed_filters = ['language'=>['table'=>'node__field_v_language','condition_field'=>'field_v_language_value','operator'=>'IN'],
    'country'=>['table'=>'node__field_v_country','condition_field'=>'field_v_country_value','operator'=>'IN'],
    'city'=>['table'=>'node__field_v_locations','condition_field'=>'field_v_locations_target_id','operator'=>'IN'],
    'theme'=>['table'=>'node__field_v_themes','condition_field'=>'field_v_themes_target_id','operator'=>'IN'],
    'tag'=>['table'=>'node__field_v_tags','condition_field'=>'field_v_tags_target_id','operator'=>'IN'],
    'chId'=>['table'=>'node__field_v_ch_id','condition_field'=>'field_v_ch_id_value','operator'=>'IN'],
    'publishedBefore'=>['table'=>'node__field_v_published','condition_field'=>'field_v_published_value','operator'=>'>'],
    'publishedAfter'=>['table'=>'node__field_v_published','condition_field'=>'field_v_published_value','operator'=>'$record){
      $alias = $q.'_als';
      $condition_field = $alias.'.'.$record['condition_field'];
      $configuration = [
        'type'       => 'LEFT',
        'table'      => $record['table'],
        'field'      => 'entity_id',
        'left_table' => 'node_field_data',
        'left_field' => 'nid',
        'operator'   => '=',
      ];

      $join = \Drupal\views\Views::pluginManager('join')
        ->createInstance('standard', $configuration);
      $rel = $query->addRelationship($alias, $join, 'node_field_data');
      $query->addTable('node__field_v_language', $rel, $join, $alias);
      $query->addWhere('', $condition_field, [$_GET[$q]], $record['operator']);
    }
  }
}

Tags:

Views

8

Hooks