Skip to content

Instantly share code, notes, and snippets.

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->preview= TRUE;
$view->is_cacheable = FALSE;
if(isset($args) && !empty($args)){
//Here we can alter the order
//$view->set_item('default', 'sort', 'random', NULL);
$view->add_item('default', 'sort', 'node', 'created',array('order' => $alter['node_created_order']));
//Exposed values
// $filters = $view->display_handler->get_option('filters');
foreach($alter['exposed'] as $key => $valor){
$view->exposed_input[$key] = $valor;
$output = $view->display_handler->preview();
return $view->result;
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