How do you override a block (supposedly) made by a view?
I can't seem to use the standard approach to override certain types of blocks - specifically blocks that are produced by a view (views module). I'm using Drupal 6. Using block.tpl.php is NOT working. In fact, a block created by a view doesn't even seem to go through this file.
So for example, say I create a view called hello_world
When you create a block with a view you get html produced similar to what is shown below (slightly abridged for brevity):
<div id="block-views-hello_world-block_2" class="block block-views region-odd even region-count-1 count-4">
<div class="block-inner">
<h2 class="title">Hello World!</h2>
<div class="content">
<div class="view view-hello_world view-id-hello_world view-display-id-block_2 view-dom-id-1">
<div class="view-content">
<span class="field-content">Do you know how to do this?</span>
</div>
</div>
</div>
</div></div> <!-- /block-inner, /block -->I need to theme and alter the block, starting with the top div shown above that is supposedly created by a block (i.e. id="block-views-hello_world-block_2").
If I follow (the excellent) instructions on overriding a view, I can only affect the content within the div containing the class "view-hello_world". This div is deeply nested inside the div structure shown above.
I need to theme/override the contents of the entire block which contains the view output. Notice there is content outside of the div containing the views output.
Specifically I was trying to add a class to the starting div for the block - something like: block-views-hello_world-block
So in my theme directory I edited block.tpl.php to create the desired class and all blocks created through the block module were affected in the expected manner. Blocks created through views were not affected at all.
It seems that drupal does NOT check block.tpl.php for a block created by a view.
I tried these combinations to no avail...
block-views-hello_world.tpl.php
block-hello_world.tpl.php
Only calling the file "block-views-hello_world-block_2.tpl.php" works. But then this is specific for the block corresponding to delta=2 only. I would really like to have a solution that would apply to all blocks created by this view, not just delta=2.
Thank you if you've read this far. Any ideas how to solve this?

theme override in view
1) Go to /admin/build/views/edit/hello_word
2) Click the block display on the left side of the page
3) Click the theme information (left side/ vertical center)
4) you will see a list of all your fields each with an alternate template name
all you have to do is copy the one you would like from /sites/all/modules/views/themes/
to your theme directory. Dont forget to name it as the theme information suggest.
click the 'scan templetes' at the bottom.
Do your changes to the new template file as needed
save the block view (important)
That should do it!
Maozet
www.drupaldir.com
theme overrides in view only affect view output not entire block
Thank you maozet for your reply, but I don't think this solves the problem.
What you describe is similar to the instructions for overriding a view.
If you look at all the template names in the theme information section of the edit tab for a particular view, the possible templates all start with...
views-viewThose templates seem to only affect the views related divs starting with "views-view".
I need to be able to affect the block as a whole.
For example, if for this block created by a view you wanted to include a title via the block admin UI or perhaps wanted to include some php customization for additional content, this additional content would not be themed by any of the "views-view" templates. It would sit within the block created by a view, but it would be either before or after the views related divs.
Normally, the standard method for block theming includes:
However block.tpl.php and block-[module].tpl.php in my testing experience is ignored when the block is created by a view.
The only one that worked for me is block-[module]-[delta].tpl.php but this forces you to make a custom block-[module]-[delta].tpl.php file for each [delta] (i.e. for each block display in that view) which is not very scalable, efficient, nor consistent with the standard method shown above.
Have you tried devel module to find candidate tempate files?
Anyway, I think you should try the theme: information on your view.
this gives you lots of candidate template files.
also: take a look here: http://stackoverflow.com/questions/1271297/drupal-views-blocks-and-template
Maozet
www.drupaldir.com
Maozet is correct, the Devel
Maozet is correct, the Devel module will show you a list of candidate files to use.
I assume you also rebuilt your theme registry? Again, Devel can make this easier.
Does your theme already have a block.tpl.php template override? (Meaning: copy an example block.tpl.php before making your custom one).
This is the example output from Devel when the 'themer info' is enabled. The left most is the most specific.
Template called:
block.tpl.php
File used:
modules/system/block.tpl.php
Candidate template files:
block-views-new_block-block_1.tpl.php < block-views.tpl.php < block-left.tpl.php < block.tpl.php
Just to check, I made a template named block-views-new_block-block_1.tpl.php - and I was able to override the entire block.
Devel will be a big help to you, I think.
edit: oh i see what you mean, it only overrides specific blocks, or all blocks made by all views, not a block of a specific view type. hmm... never noticed that!
- Tips on Giving effective feedback in the forums
I just found the need to make
I just found the need to make a custom tpl for a set of blocks I made with views.
In other words:
block-views-viewname.tpl.php is essentially what we would ideally want to do.
Instead we have to do:
block-views.tpl.php
<?php
// $Id: block.tpl.php,v 1.1.2.2.2.4 2009/01/16 21:53:32 tombigel Exp $
/**
* @file block.tpl.php
*
* Theme implementation to display a block.
*
* Available variables:
* - $block->subject: Block title.
* - $block->content: Block content.
* - $block->module: Module that generated the block.
* - $block->delta: This is a numeric id connected to each module.
* - $block->region: The block region embedding the current block.
*
* Helper variables:
* - $block_zebra: Outputs 'odd' and 'even' dependent on each block region.
* - $zebra: Same output as $block_zebra but independent of any block region.
* - $block_id: Counter dependent on each block region.
* - $id: Same output as $block_id but independent of any block region.
* - $is_front: Flags true when presented in the front page.
* - $logged_in: Flags true when the current user is a logged-in member.
* - $is_admin: Flags true when the current user is an administrator.
*
* Tendu Specific:
* - $block_region_placement: Outputs 'block-first and 'block-last' to the first and the last blocks on each block region.
*
* Suport for "block-class" and "block-theme" modules (Not included in the theme):
* - $blocktheme: Blocktheme's machine readable block name.
* - block_class($block): Block classes defined in admin/build/block
*
* @see template_preprocess()
* @see template_preprocess_block()
*/
// Here we override specific blocks based on their name and delta, more specifically by which view provides the block
// There is a limitation in Views module which keeps one from doing block-views-[VIEWNAME].tpl.php for all blocks from a particular view.
// As a workaround we simply check the VIEWNAME here to override the template as needed.
?>
<?php // grab the block->delta and strip off the number at the end
$blockdelta = substr($block->delta, 0, (strrpos($block->delta, "_")));
// create a blockname to be used for our switch
$blockname = "block-{$block->module}-{$blockdelta}";
// this switch uses any views block's id (without the numerical value that is at the end)
switch ($blockname):
case "uc_products-block": ?>
<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="block block-<?php print $block->module ?> <?php print $block_region_placement ?> block-<?php print $block_zebra ?> <?php if ($blocktheme != '') print $blocktheme; if (function_exists(block_class)) print block_class($block); ?>">
<?php if ($block->subject): ?>
<h2><?php print $block->subject ?></h2>
<?php endif;?>
<?php print $block->content ?>
</div><?php
break;
default: ?>
<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="block block-<?php print $block->module ?> <?php print $block_region_placement ?> block-<?php print $block_zebra ?> <?php if ($blocktheme != '') print $blocktheme; if (function_exists(block_class)) print block_class($block); ?>">
<?php if ($block->subject): ?>
<h2><?php print $block->subject ?></h2>
<?php endif;?>
<div class="content">
<?php print $block->content ?>
</div>
</div><?php
break;
endswitch;
?>