Programmatically render a Drupal view with exposed filters, arguments, etc.

Embedding a view programmatically is not too difficult, but what about passing it a value for an exposed filter? This snippet gives a simple example of how to modify the View object in a more meaningful way before rendering it.

$name = 'my_view';
$display = 'block_1';
$view = views_get_view($name);

If you'd like to pass in exposed filter values, define these properties before calling $view->preview():

// The $exposed_filters keys must correctly correspond with the actual keys of your view's exposed filters.
$exposed_filters = array(
  'keys' => 'sail',
  'term_node_tid_depth' => 'All',
// Pass in exposed filter values. array_merge() order prioritizes user input.
$view->exposed_input = array_merge($exposed_filters, (array)$view->exposed_input);
$view->exposed_raw_input = array_merge($exposed_filters, (array)$view->exposed_raw_input);
$view->exposed_data = array_merge($exposed_filters, (array)$view->exposed_data);

Print the view!

$output = $view->preview();
if ($view->result) {
  print $output;

is a solution also for drupal 6.x?

which is a solution for to do this :

I have three views page (that are 3 horiz menus in the header)
V1 V2 V3;
on V3 I created one exposed filter F3 (a select); the block is in the header so have
V1 V2 V3

so the filter is only for V3;
how can instead have a common filter
V1 V2 V3

so when click on V1 is applied own view with the filter value already selected in F123;
or if I am on V1 or V2 .. if I change the selection in F123 the view V1 or V2 ... are reloaded and filtered by the new value

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.