Created
November 2, 2012 12:42
-
-
Save rafinskipg/4001140 to your computer and use it in GitHub Desktop.
Creating views Drupal 7 programatically, with exposed filters and order by
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice, thanks for sharing!