I have a page view with exposed filters and would like to print said filters into a custom block in order to add extra html markup above the actual form. This block will be used on other pages using Context.

I know I can create a block by setting 'exposed filters in block' to yes, however that's not the solution I am looking for.


Hours and hours of searching and nothing was working. Several minutes after posting this question, I figured it out. The following code can be used in template files or blocks.

  $block = module_invoke('views', 'block_view', '-exp-VIEW_NAME-DISPLAY_NAME');
  print render($block['content']);
| improve this answer | |
  • There are so many ways to manage this with code. Nice finding though. – niksmac Nov 1 '12 at 7:34
  • 2
    Yeah, there were tons of other very complex solutions out there! This one is short and simple. Granted I am sure there are use cases for the other solutions. – filmoreha Nov 1 '12 at 7:37
  • 1
    On another note, thanks for coming back to tell people your solution! – Chapabu Nov 1 '12 at 9:48

Its simple take a look at the screenshot. enter image description here

| improve this answer | |
  • I understand thats one way to do it, but that wasn't the solution I was looking for. I needed to be able to add HTML markup above the actual form. Thanks though. – filmoreha Nov 1 '12 at 7:27
  • @filmoreha Well. A simple tweak is to make a module with custom markup and handle it same way like this form via contextual filters or something.. I am also looking for a better solution. – niksmac Nov 1 '12 at 7:32

For Drupal 7:

$view = views_get_view('view_name');
$display_id = 'page'; // Display: page/block
$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,
    'always_process' => TRUE, // This is important for handle the form status.
$form = drupal_build_form('views_exposed_form', $form_state);
print drupal_render($form);

'always_process' is main form state argument for handle the exposed filter form status.

From https://www.drupal.org/node/2854114

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.