| 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 996 - 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
Custom sorting ins views
Permalink<?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";}
}
?>
hook_views_query_alter vs hook_views_pre_execute ?
PermalinkHi All,
What is the main difference between these two? when to use hook_views_query_alter? when to use hook_views_pre_execute?
Drupal 8
PermalinkIf you are looking for this function in Drupal 8, see hook_views_query_alter(ViewExecutable &$view, QueryPluginBase &$query)
Views_plugin_query_default
PermalinkAs recommended in this post, it would be beneficial to check out this documentation regarding views_plug_in_query_default.
MODULE.views.inc
PermalinkI 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...
versions
PermalinkI forgot to say that I'm using D7 and Views 3.7, in case that matters...