3

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.

3 Answers 3

8

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.

<?php
  $block = module_invoke('views', 'block_view', '-exp-VIEW_NAME-DISPLAY_NAME');
  print render($block['content']);
?>
3
  • There are so many ways to manage this with code. Nice finding though.
    – niksmac
    Commented Nov 1, 2012 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
    Commented Nov 1, 2012 at 7:37
  • 1
    On another note, thanks for coming back to tell people your solution!
    – Chapabu
    Commented Nov 1, 2012 at 9:48
1

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

2
  • 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
    Commented Nov 1, 2012 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
    Commented Nov 1, 2012 at 7:32
1

For Drupal 7:

$view = views_get_view('view_name');
$display_id = 'page'; // Display: page/block
$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,
    '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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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