To me it seems that we're a bit limited in controlling the positioning of specific blocks in themes. Yes we can define the location of regions, but beyond that, it doesn't seem you have much else control. (I know you can theme the output of specific or general blocks, but I'm talking more the positioning on the page). Am I missing something here?

For example, on the front page, I might want to have a bunch of different blocks (in the content area) in two different columns (blocks might include news, upcoming events, recent minutes, etc). Would each column have to be a different region? And then on different pages (or content types, etc) I wanted to have different blocks except in a sidebar. This could be a different region, but what if I wanted to have one common block, such as the news block. If it already belonged to the "front_bottom_left" region, then it can't also belong to the "left_sidebar" region too, right?

It would seem very useful to me if we could print specific blocks in the theme, just like we print specific regions, like...

<?
if ($blocks['block_name']) {
  print $blocks['block_name'];
}
?>

...or something like that. But I don't believe that's possible.

What is the best way of arranging blocks besides creating a huge number of regions, especially with different arrangements based on node or content type? And what if a block needs to be displayed in multiple arrangements / regions?

Thanks so much!

Comments

Jeff Burnz’s picture

You can print blocks directly, google more :) There's even a handbook page about this, http://drupal.org/node/499274, so yeah, rtm man ;)

What you loose is the ability to easily move a block, if thats not a problem then do it.

Panels and Context modules can overcome the one block/one region problem.

adam.hastings’s picture

Thanks for the reply!

I found something like this:

$block = (object) module_invoke('user', 'block', 'view', 2);
print theme('block', $block);

Is this what you were talking about? It does work, except this is part of the code output...

<div id="block--" class="clear-block block block-">

...so there seems to be a problem creating the block id. Is this what is supposed to happen?

I will also look into Panels and Context more.

Thanks!

Jeff Burnz’s picture

The class is missing also block-, not sure why its not there. Will investigate... what theme are you using?

FWIW you can print just the content of the block:

$block =  module_invoke('user', 'block', 'view', 2);
print $block['content'];
adam.hastings’s picture

This was in Garland. I'm in the middle of developing my own theme, so it'll eventually be used in there, but it's not ready to be used yet.

adam.hastings’s picture

I believe I found the source of the problem. The default template for blocks (modules/system/block.tpl.php) includes this line...

<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="block block-<?php print $block->module ?>">

...but the module_invoke() function retrieves an object that only contains subject and content. Since the module and delta are both included in the module_invoke() arguments, you can just add those to the object.

For anyone else who sees this, here's a simple function that can be put in template.php to do this:

function block_retrieve($module, $delta) {
  $block = (object) module_invoke($module, 'block', 'view', $delta);
  $block->module = $module;
  $block->delta = $delta;
  return theme('block', $block);
}

Then in template files you can simply do something like:

<?php print block_retrieve('views', 'News-block_1'); ?>

Also notice the delta for a block created by Views - it's not just a number. It's [View name]-block_[block number].

Thank you Jeff for your help!

Jeff Burnz’s picture

adam.hastings’s picture

I have been playing with Panels a bit, and I get the general idea but am having one problem. For some reason, blocks are not showing in panels, whether they're system or module blocks (like Who's Online) or ones generated by a View. There is just blank space in the block's place. Navigation menus and such work, but not blocks. Any ideas? Thanks!

adam.hastings’s picture

Nevermind.... I think I solved the inserting block issue (see above) so I don't think I'm going to use Panels in this site anyways... maybe one in the future because they do look very promising.