Programmatically outputting a Drupal 6 block
Share with Others
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.