Placing the contents of a block in any location
PLEASE NOTE These snippets are user submitted. Use at your own risk. For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.
If you want the contents of a block to appear within a page you can use the following snippet:
<?php
$block = module_invoke('module_name', 'block', 'view', 0);
print $block['content'];
?>To specify which block from which module, you simply edit the two following variables in the first line:
module_name = The name of the module
The number = Is the $delta of the block
For example to call the block on the Font Size module use:
<?php
$block = module_invoke('fontsize', 'block', 'view', 0);
print $block['content'];
?>Or for example to display who is a new user:
<?php
$block = module_invoke('user', 'block', 'view', 2);
print $block['content'];
?>Showing more than the block body
Calls to $block['content'] only provide the block body. Here's 2 ways to provide more.
1. In comments, GWL shows how to wrap the $block data in divs. It also checks for empty content and hides empty blocks:
<?php
$block = module_invoke('block', 'block', 'view', 1);
if ($block['content']) {
$output = "<div class=\"front-page-block\">\n";
$output .= "<div id=\"front-block-title\"><h2>".$block['subject']."</h2></div>\n";
$output .= "<div class=\"content\">".$block['content']."</div>\n";
$output .= "</div>\n";
print $output;
}
?>2. On a different page japanitrat shows how to call theme() function with a cast to object.
<?php
$block = (object) module_invoke('[target_module]', 'block', 'view', "[target_block_ID]");
print theme('block', $block);
?>The theme call will provide the block wrapped in your theme's block.tpl output (assuming php template). However, it may not look exactly like a block in a given sidebar (say, block appearing in center content, but rendered as if a left sidebar block). Usually this difference comes from the absence of variables determining HTML and CSS styling written in the block.tpl file.
To make a block appear in one place, themed as if for another region, that would require inspection of your theme's template.php that is outside the scope here. For an example, review the Acquia Marina template.php, line 302, 'template_preprocess_block(), for where the 'rounded block' CSS class comes from, then triangulate with that theme's block.tpl.php.

Placing blocks outside sidebars
This is a GREAT snippet ... but I ran into a couple of problems when I started using it. Here are my solutions:
Problem: The snippet only prints the contents of a block, and not the block itself as it would normally appear in the sidebar. Primarily, I needed the title of the block to show, and I needed to enclose the block in a div for styling.
Solution: Just as
$block['content']calls the content of the block,$block['subject']calls the title of the block as defined in admin>>blocks>>configure. To style it, I wrapped the whole thing in a set of divs (as done on the sidebars) and wound up with this:<?php$block = module_invoke('block', 'block', 'view', 1);
$output = "<div class=\"front-page-block\">\n";
$output .= "<div id=\"front-block-title\"><h2>".$block['subject']."</h2></div>\n";
$output .= "<div class=\"content\">".$block['content']."</div>\n";
$output .= "</div>\n";
print $output;
?>
Problem: In the sidebars, Drupal automatically hides blocks that don't have content. That doesn't happen when you pull a block elsewhere on the page. Specifically, though there's no content to show, the block title and the div formatting still display. The result is an ugly empty block.
Solution: After a little trial and error, I settled on a simple IF statement to check if
$block['content']was empty:<?php$block = module_invoke('block', 'block', 'view', 1);
if ($block['content']) {
$output = "<div class=\"front-page-block\">\n";
$output .= "<div id=\"front-block-title\"><h2>".$block['subject']."</h2></div>\n";
$output .= "<div class=\"content\">".$block['content']."</div>\n";
$output .= "</div>\n";
print $output;
}
?>
Now my non-sidebar blocks look great, and if there's no content to show, they don't display. Just like the real thing!
Gary
How to only show child pages?
I'm trying to show only child pages, like this very own drupal site does on this page: http://drupal.org/node/23220 (the child links within the page are redundant with the links on the left side.)
<?php$block = module_invoke('user', 'block', 'view', 1);
print $block['content'];
?>
The code above shows ALL the navigation links, but I'm trying to figure out how to restrict it to just child pages. My goal is ultimately to remove the 'expanded' menu items somehow and have it just show the child pages in the middle of the page.
Any help is apreciated!
Trent
Appearantly $delta is not always a number...
I tried to use this php code to show the feed-content of an aggregator feed on a page. It turned up to work out this way:
<?php$block = module_invoke('aggregator', 'block', 'view', 'feed-1');
print $block['content'];
?>
So $delta here is supposed to be of the form 'feed-x' where x stands for the number of the feed.
module & delta
Yes, sometimes delta is an integer, other times it is a string. If you have PHPMyAdmin (or some other way to view your db tables), look at the blocks table. In there you will find columns for the module and delta of every block you have created. The module and delta values can be used in the snippets above. Just be sure to pick one that uses your current theme.
It is also possible to use
It is also possible to use the standard block-templates to display the blocks:
<?php$block_module = 'your-module';
$temp_block = module_invoke($block_module, 'block', 'view', 0);
$block->subject = $temp_block['subject'];
$block->content = $temp_block['content'];
include( path_to_theme() . '/block-' . $block_module . '.tpl.php' );
?>
place a block in a page
I've been looking for the answer to this for a long time, so thanks for posting it here.
I was able to create a block with with some menu info. And then place it into a few pages on my site without any theming. First I made a new block and noted it's number (it was block/8)
Then I went to the page where I wanted the little mini-menu and put this in:
<?php$block = module_invoke('block', 'block', 'view', 8);
print $block['content'];
?>
It showed just fine, and now I only have to edit my mini-menu in one spot if I need a change.
Thanks
add a nice menus block to page
Cool. You can do it with Nice menus.
<?php$block = module_invoke('nice_menus', 'block', 'view', 1);
print $block['content'];
?>
I also created a new content type, added a field to the top. And then inserted the code for the nice menus block there. Now I have an editable menu within my page.
nice
This seems fine and all but
This seems fine and all but what if you want to style everything ? Lets say an article.
The block outputs:
Title
Submitted by author on date
Text
Comments
How do you output this manually so you can style it ? Because i want to output the block inside a graphic so i need to position everything in detail.
How do you output for example the "Submitted by author date" for example ? Or just the headline ?
this code does explain
this code does explain things - print $block['content'];
it means that it outputs one part of the array, namely "content"
if you investigate other segments of the array and their names, you can output them separately, like print $block['subject']; etc.
I would like to move the
I would like to move the login block from the sidebar last to the sidebar first after log in.
Where would I look for this syntax?
thanks
lucky
Move Login block
In D5 it's the weight settings - moves it up . In D6 just grabe the cross and drag it where you want it
Regards
Ron
Caching
Does this method (module_invoke('user', 'block', 'view', 2);) ensure that the block caching mechanism still applies?
For example, if a block is cached for a user: will the method discussed above ensure that the block is not rebuilt when the same user views it twice? I think this is a critical performance question...