Drupal 7 – Render Views Exposed Filter programmatically

Update at 2015-04-27: Another suggestion by Petu which could render the filter as a block programmatically.

Update at 2014-09-18: An simpler way to do that is to enable the Exposed form in block in the view settings and then render the block as suggested by plano.com.

The following piece of code render the Views Exposed Filter.

$view = views_get_view('<view name>');
$display_id = 'page';
$view->set_display($display_id);
$view->init_handlers();
$form_state = array(
  'view' => $view,
  'display' => $view->display_handler->display,
  'exposed_form_plugin' => $view->display_handler->get_plugin('exposed_form'),
  'method' => 'get',
  'rerender' => TRUE,
  'no_redirect' => TRUE,
);
$form = drupal_build_form('views_exposed_form', $form_state);
print drupal_render($form);

 

Done =)

Reference:

Advertisements

4 thoughts on “Drupal 7 – Render Views Exposed Filter programmatically”

  1. Much more easiest way is:

    $block = module_invoke('views', 'block_view', '-exp--');
    print $block['content'];
    

    Select the Exposed form option as “Exposed form in block : Yes”
    by exposing the selected fields under the “Filter Criteria”.

    Like

    1. More correct way is:

      $block = module_invoke('views', 'block_view', '-exp-search_page-page');
      print render($block['content']);
      

      Where ‘-exp-search_page-page’ is a block delta. It may be taken from URL on block editing page.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s