Skip to content

Instantly share code, notes, and snippets.

@kevinchampion
Created December 14, 2011 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinchampion/1475316 to your computer and use it in GitHub Desktop.
Save kevinchampion/1475316 to your computer and use it in GitHub Desktop.
Using hook_views to programmatically filter a view in Views 3, Drupal 7
function lsadataclassify_views_pre_view(&$view, &$display_id, &$args){
if ($view->name === 'protection_measures' && $view->current_display === 'page'){
}
}
function lsadataclassify_views_pre_view(&$view, &$display_id, &$args){
if ($view->name === 'protection_measures' && $view->current_display === 'page' && isset($args[0])){
$node = node_load($args[0]);
$sensitivity = $node->field_research_sensitivity[$node->language][0]['value'];
$tids = array();
switch ($sensitivity) {
case 'high':
$tids = array(5, 6, 7);
break;
case 'moderate':
$tids = array(6, 7);
break;
case 'low':
$tids = array(7);
break;
}
$view->display['page']->handler->options['filters']['field_pm_sensitivity_termref_tid']['value'] = $tids;
}
}
$view->display['page']->handler->options['filters'];
$view = views_get_page_view();
$pm_nodes = array();
// loop through each row of the view which contains a node
foreach ($view->result as $key => $entity){
// node of protection measure
$pm_nodes[$key] = $entity->_field_data['nid']['entity'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment