Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

How to modify an existing views join using hook_views_query_alter in Drupal, Alter the query before it is executed.

This will let you alter the query before you execute the query so you have a chance to request a different set of data than originally configured within the views.

First, check the tables that you have in your query, it could be a table alias or a table name. The following code will show you the list of tables.


Then you know what table you need to alter so do the following

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

  if ($view->storage->get('id') === 'VIEWNAME' && $view->current_display === 'DISPLAY') {

    $table = $query->getTableInfo('MY TABLE NAME');
    $table['join']->extra = [];
    // Example
    $table = $query->getTableInfo('vehicle__field_make_model_value_0');
    $table['join']->extra[] = [
      'field' => 'title',
      'value' => 'management',
      'operator' => '!='


Example 2 Add condition in query alter views

function Module_views_query_alter(ViewExecutable $view, QueryPluginBase $query)
    switch ($view->storage->id()) {
        case 'Page':
            if ($view->current_display == 'block_1') {
                $node = \Drupal::routeMatch()->getParameter('node');
                $categoryId = $node->get('field_category')->target_id;
                $query->addField('taxonomy_term_field_data', 'tid');
                $query->addWhere('conditions', 'taxonomy_term_field_data.tid', $categoryId, '!=');


Api: hook_views_query_alter.


Submitted by Shiq (not verified) on Thu, 11/26/2020 - 18:32 Permalink

Hi, I just wanted to thank you for providing such good code. Works great!
Submitted by Regina (not verified) on Mon, 03/01/2021 - 19:42 Permalink

I have a Media-Entity-View to build an entity Browser. In this case, there is an additional field in the media-type, where can be choosed a user id. And the condition in view says: The field with user-id can be empty. In this case no filter is active. Or there are one ore more user choosed, which can see this image in entity browser. I did it with a views_query_alter function. function pdf_options_views_query_alter(Drupal\views\ViewExecutable $view, Drupal\views\Plugin\views\query\Sql $query){ if ($view->id() == 'media_entity_browser') { $userid = \Drupal::currentUser()->id(); $query->addWhere('2', 'media__field_gesellschafter.field_gesellschafter_target_id', $userid , '='); } } The SQL of the view looks like this: SELECT media_field_data.mid AS mid FROM {media_field_data} media_field_data LEFT JOIN {media__field_gesellschafter} media__field_gesellschafter ON media_field_data.mid = media__field_gesellschafter.entity_id AND media__field_gesellschafter.deleted = '0' WHERE (media_field_data.bundle IN ('bilderpool_75x110')) AND ( (media__field_gesellschafter.field_gesellschafter_target_id IS NULL) OR (media__field_gesellschafter.field_gesellschafter_target_id = '33') ) LIMIT 9 OFFSET 0 I would like to know, why my solution works. ;-) It does not work with this $query->addWhere('OR', 'media__field_gesellschafter.field_gesellschafter_target_id', $userid , '='); I did not find a good documentation for the syntax of $query->addWhere.
Add new comment

Restricted HTML

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

I am a senior web developer, I have experience in planning and developing large scale dynamic web applications especially in Drupal and Laravel.

Web Posts


Page Facebook