Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Programmatically outputting a Drupal 6 block

Parent Feed: 

Sometimes we all need reminders on the seemingly simple things. This is one of those snippets of code that I find myself needing from time to time. I decided to put it here so I can easily find it in the future. Hopefully it can help you too. I borrowed most of the code/concepts from the comments at http://api.drupal.org/api/drupal/developer!theme.php/function/theme_block/6.

The below PHP code provides examples of how to easily display a Drupal 6 block using code. This may be something you put in a Drupal 6 template file or a Drupal 6 theme function.

If you just want to get the content of the Drupal 6 block, you can use the following code:

$block = module_invoke('MYMODULE', 'block', 'view', 0);
print $block['content']

If you want to print the themed version of the Drupal 6 block, you can use the following code:

// The name of the module implementing hook_block.
$module = 'MYMODULE';
 
// The delta of the block, may be a number such as 0 or 1, or may be a string 
// such as 'mymodule_block' depending on how hook_block was implemented.
$delta = 0;
 
// Get the block as an object and set the module and delta properties.
$block = (object) module_invoke($module, 'block', 'view', $delta);
$block->module = $module;
$block->delta = $delta;
 
// Now you can print the themed block.
print theme('block', $block);

Simple, but useful.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web