Expose a view's filters both in a block, and in the actual view

I made a View, and I exposed the filters in a block; then, in the block's administrative screen, I set this block not to appear in the view that belong. In the actual view I don't want to show this block. Instead, I want the filters to appear in the content as they normally would if I didn't expose them in a block.

If that makes any difference, I am using Drupal 7.

Answers 4

  • My understanding is that your view has exposed filters and that Exposed form in block is set to Yes.

    To put the exposed form where it would appear if it weren't in block, you should be able to place the exposed form block into your Content region, above the view itself.

    To have the exposed form displayed in both the view and a separate block, leave the form in the view and create a PHP block with the following body:

    <?php
      if ($block = module_invoke('views', 'block', 'view', '-exp-YOUR_VIEW-YOUR_DISPLAY') ) {
        print $block['content'] ;
      }
    ?>
    

  • The accepted answer did not work with Drupal 7 for me. I followed http://www.drup-all.com/blog/exposed-filters-form-your-custom-block-drupal-7 with success. Code below:

    /** implements hook_block_info() **/
    function test_block_info(){
      $blocks = array();
      $blocks['custom_exposed_filter_form'] = array(
        'info' => t('Exposed filter form'),
        'cache' => DRUPAL_NO_CACHE,
      );
      return $blocks;
    }
    
    /** implements hook_block_view() **/
    function test_block_view($delta = ''){
      $block = array();
      switch($delta){
        case 'custom_exposed_filter_form':
          $block['subject'] = t('Exposed filter form');
          $block['content'] = _test_get_exposed_filter_form();   
        break;
      }
      return $block;
    }
    
    /** Function to get the Exposed filter form **/
    function _test_get_exposed_filter_form(){
      // Get a view from the database or from default views . "test" is the view name.
      $view = views_get_view('test');
      // Set the display as current.
      $view->set_display('page');
      // Acquire and attach all of the handlers to the view.
      $view->init_handlers();
      $exposed_form = $view->display_handler->get_plugin('exposed_form');
      return $exposed_form->render_exposed_form();
    }
    

  • $block = block_load('views', '-exp-VIEW_NAME-VIEW_DISPLAY');
    $output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block)))); 
    echo($output);
    

  • There is a Views UI alternative to the accepted answer, at least with Views 3 / D7:

    • configure your view with the view exposed form you'd like to see on the view page
    • Clone that view and for sake of future remembering, call it search block
    • In the search block view, in advanced setup, set Show the exposed form in a block : Yes
    • It 's important that both views have the same path
    • Display the block where ever you'd like to

    It's worth saying that with this method you can now edit the search block and remove some filters, add markup... (but no filters that would not be in the mother view I believe).

    You can even reproduce steps and have different custom blocks pointing to the same view.

    I've done that recently and found it very useful


Related Questions