Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating views Drupal 7 programatically, with exposed filters and order by
$view = 'my_view_name';
$display = 'my_display';
$alter = array('exposed' => array('title' => 'title I search'), 'node_created_order' => 'ASC');
//Simple results of a view
public function getResults($view, $display, $args = NULL){
return views_get_view_result($view, $display, $args);
}
//View with exposed filters and more things
public function getAlteredResults($name, $display, $args, $alter){
$view = views_get_view($name);
$view->set_display($display);
$view->preview= TRUE;
$view->is_cacheable = FALSE;
if(isset($args) && !empty($args)){
$view->set_arguments(array($args));
}
if(isset($alter['items_per_page'])){
$view->display_handler->set_option('items_per_page',$alter['items_per_page']);
}
//Here we can alter the order
//$view->set_item('default', 'sort', 'random', NULL);
if(isset($alter['node_created_order'])){
$view->add_item('default', 'sort', 'node', 'created',array('order' => $alter['node_created_order']));
}
//Exposed values
if(isset($alter['exposed'])){
// $filters = $view->display_handler->get_option('filters');
foreach($alter['exposed'] as $key => $valor){
$view->exposed_input[$key] = $valor;
}
}
$view->pre_execute();
$output = $view->display_handler->preview();
$view->post_execute();
return $view->result;
}
@krisimmig
Copy link

krisimmig commented Jan 15, 2015

nice, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment