function hook_views_query_alter

You are here

7 views.api.php hook_views_query_alter(&$view, &$query)
6 docs.php hook_views_query_alter(&$view, &$query)

Alter the query before executing the query.

This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. MODULENAME.views.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified.

Parameters

$view: The view object about to be processed.

$query: An object describing the query.

See also

hook_views_query_substitutions()

Related topics

1 invocation of hook_views_query_alter()
views_plugin_query_default::alter in plugins/views_plugin_query_default.inc
Let modules modify the query just prior to finalizing it.

File

./views.api.php, line 1070
Describe hooks provided by the Views module.

Code

function hook_views_query_alter(&$view, &$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->name == '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, chang the
        // condition to filter on node ID.
        if ($condition['field'] == 'node.title') {
          $condition = array(
            'field' => 'node.nid',
            'value' => $view->exposed_raw_input['title'],
            'operator' => '=',
          );
        }
      }
    }
  }
}

Comments

<?php
/**
* Implementation of hook_views_query_alter
* @param type $view
* @param type $query
*/
function mymodule_views_query_alter(&$view, &$query) {

 

//simple example: change the order of the master display
  //if you want to do it only  on a certain display add something
  // like  "&&$view->current_display == 'panel_page_1'"
 
if ($view->name == 'VIEWNAME') {

   

//to find out what the fieldname ist: use devel and add your desired field as
    //first filter ( =>orderby[0]). install devel and uncomment the next line
    //dpm($view->query->orderby)

   

$view->query->orderby[0]['field'] = "CASE FIELD_NAME WHEN 'apple' THEN 1 WHEN 'zebra' THEN 2 WHEN 'banna' THEN 3 ELSE 4 END";
  }
}
?>

Hi All,
What is the main difference between these two? when to use hook_views_query_alter? when to use hook_views_pre_execute?

If you are looking for this function in Drupal 8, see hook_views_query_alter(ViewExecutable &$view, QueryPluginBase &$query)

As recommended in this post, it would be beneficial to check out this documentation regarding views_plug_in_query_default.

I tried to put my function (mymodule_views_query_alter) in mymodule.views.inc as per instruction at the top of the function documentation, but it didn't work...my hook never got executed.

I disabled and re-enabled my module several times, cleared the cache and even added "files[] = mymodule.views.inc" in mymodule.info but nothing worked.

So instead, I put my function in mymodule.module, restarted my module and that worked.

Hope this will save others some headache...

I forgot to say that I'm using D7 and Views 3.7, in case that matters...

Hi,

I had the same issue and I just cleared my cache. I personally used Drush, but you should be able to do it from the Admin side inside Drupal.

Have a nice day
Frank N.

To get it work, you need to implement hook_views_api().

This hook will be invoked even if 'Disable SQL rewriting' is enabled for the view. That's not necessarily obvious. In my case, this was a good thing.

Thanks Permalink. I have the same situation. I have hook_views_api in .inc file but still not working. I follow Permalink's advice and it works.
I suggest Views 3 author update the docutmentation here or fix the bug:
https://api.drupal.org/api/views/views.api.php/function/hook_views_query...