public
Created

Views query alter

  • Download Gist
query_alter_example.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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',
);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.