Skip to content

Instantly share code, notes, and snippets.

@rafinskipg
Created November 2, 2012 12:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafinskipg/4001074 to your computer and use it in GitHub Desktop.
Save rafinskipg/4001074 to your computer and use it in GitHub Desktop.
Views query alter
function mymodule_views_query_alter(&$view, &$query) {
if ($view->name == 'myview' ) {
//Filter taxonomy by language
global $language;
$query->where[] = array(
'conditions' => array(array(
'field' => 'taxonomy_term_data.language',
'value' => array($language->language),
'operator' => 'in',
)),
'args' => array(),
'type' => 'AND',
);
}elseif($view->name =='other_view' && $view->current_display == 'page'){
//Add another table to the relation
$query->add_table('field_data_field_evento_ciclo_agenda');
//Get conditions and alter them
$condiciones = $query->where['1']['conditions'];
$query->where['1']['conditions'][] = array(
'field' => 'node.type',
'value' => array('event'),
'operator' => 'in',
);
$query->where['1']['conditions'][] = array(
'field' => 'field_data_field_evento_ciclo_agenda.field_evento_ciclo_agenda_nid',
'value' => array(),
'operator' => 'IS NULL',
);
//Add an extra condition
$condiciones[] = array(
'field' => 'node.type',
'value' => array('season'),
'operator' => 'in',
);
//Update conditions in query
$query->where[] = array(
'conditions' => $condiciones,
'args' => array(),
'type' => 'AND',
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment