Many a times when we are working with drupal views there comes situations when you need to alter query based on dynamic conditions, at such times drupal hook_views query becomes very handy as you can control the conditions in code. For example consider a senario where you want to add condition in views if a particular SESSION is set
function mymodule_views_query_alter(&$view, &$query) { switch($view->name) { case 'myview': if (isset(S_SESSION['mysession']) && ($view->current_display == 'page')) { $view->query->where[1][conditions][] = array( 'field' => 'node.language', 'value' => array($language->language), 'operator' => 'in' ); } break; } }
This will add condition to view query based on the SESSION value.