0

I am trying to move a views exposed filter into a custom block, and then add it back to the views header. The reason for this is because I would like to intersperse the filter with other blocks/markup in the views header. When I selected the option to turn the filter into a block, it does show in the Block UI, but not as an option to add to the view header.

Is this possible in views?

2
  • You say you want to add it to the header with other blocks. How were you planning on adding the other blocks to the view header? It should work the same way for the exposed filter block.
    – rooby
    Commented Dec 22, 2015 at 0:46
  • @rooby - thats what I thought too, but it didn't work. As I mentioned, the filter block doesn't show as an option as a block to insert into the views header. Commented Dec 22, 2015 at 16:22

1 Answer 1

1

Add the following function in your template.php

function THEMENAME_render_block($module, $delta, array $options = array()){
  $default_options = array(
    'title' => '<none>',
    'region' => -1,
  );

  $options = array_merge($default_options, $options);

  $block = db_query('SELECT * FROM {block} WHERE module = :module AND delta = :delta', array(':module' => $module, ':delta' => $delta))->fetchObject();
  if($block) {
    $block = block_load($module, $delta);
    $block->title = $options['title'];
    $block->region = $options['region'];
    $blocks = array($block);
    $rblocks = _block_render_blocks($blocks);
    $render_array = _block_get_renderable_array($rblocks);
    return drupal_render($render_array);
  }
  return "";
}

then in your views header render the block like so

<?php print THEMENAME_render_block('<moduleName>', 'delta', array('title' => t('Filter Title'))); ?>

For this you must have views php module enabled if you want to render the block from views header or else you can use tpl file to render the block in the header as @rooby suggested.

6
  • 1
    It would be preferable to override views-view.tpl.php instead of adding PHP via the views php module. If you're already writing custom code you may as well do it all in code and not put code in the DB.
    – rooby
    Commented Dec 22, 2015 at 0:48
  • Passing in the title and region options like that seems a bit strange since you can configure those things via the block UI. If you really want to be able to override those options it should be done only if they are present and if they are not present it should use the options saved for that block in the DB. Otherwise the person using this snippet might not know why their block settings are being ignored.
    – rooby
    Commented Dec 22, 2015 at 0:55
  • @rooby Agreed, Title and regions are optional arguments You can pass it or not.
    – Shabir A.
    Commented Dec 22, 2015 at 12:24
  • @ShabirA.- adding PHP into the database is not an option for me. I've learned the hard way that that is a really bad idea and makes things hard to debug. I definitely can render the blocks in code in the views tpl, I just figured there should be a way of rendering it via the UI, since it its a Drupal block. Commented Dec 22, 2015 at 16:26
  • 1
    @user1015214 you are right. views php kills site site performance as the code are not get cached. this is the reason php module removed from drupal 8 core. You can use the same function in your views tpl instead.
    – Shabir A.
    Commented Dec 22, 2015 at 16:28

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.