How to modify an existing views join using hook_views_query_alter

You, DrupalDrupal theme

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.

API : hook_views_query_alter

If you are looking for a way to alter where conditions here is a great guide from Chapter3. Using hook_views_query_alter to alter a query.

Join queries and conditions are little different so here is how you do it.

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_custom_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' => '!='
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.