How to insert a block into a tpl or into content programmatically with Drupal

There are several ways to insert blocks onto a page with Drupal. You can use panels, you can add a region to the theme, or you can call the blocks programmatically with php. Though it might not seem evident at first there are some situations where it really is much easier, or better just to call a block with php rather than using the other options. One good example is where you want to add a particular block to a page that shows up in an odd location (such as the top right hand corner of the page) where panels can’t get to. Now say that this particular area only has room for that one block and if an administrator were to add an additional block it would break the layout. You want to make it so that only one block can exist in that area, so a region would not work. In comes php to the rescue. I am going to show you a short snippet of php that allows you to call a block from anywhere in the Drupal system and then I am going to explain how it works.<?php
$block = module_invoke(‘jquerymenu’, ‘block’, ‘view’, 0);
print $block[‘content’];
?>

To use the code above you need to understand it so that you can customize it for your own site or expand upon it (module_invoke can be used for more than just calling blocks). What module_invoke allows you to call particular hooks from a particular Drupal module and use the return value of that hook. In this case the hook that we are looking to use is hook_block(). The arguments that module_invoke accepts are as follows:

  1. Module: The official name of the module as it is written for its folder. In our example we are calling the jquerymenu module to get one of its blocks. (Note: If you are writing a word with spaces or html characters other than underscores you are using the wrong name.)
  2. Hook: The hook function that you want to invoke. such as hook_block, hook_menu, hook_cron etc… The argument should be written without the “hook_”
  3. Arguments that will be sent to that hook: There can be several arguments each separated by a comma. In the case of our example we are calling hook_block and sending it the argument “view” for the $op argument and ‘0’ for the $delta argument.

Where do I get that information for the block I am trying to call?

The easiest way is to go to the blocks page and to hover over the “configure” link. Without clicking on the link look in the bottom left hand corner of your browser (at least with Firefox) there you should see the URL that you would be taken to upon clicking. The important information that you need is in the last two elements of the URL. So if the URL of the link is admin/build/block/configure/jquerymenu/0 then the module that created that block is the jquerymenu block and the $delta (or block number) is 0.

So to review, the example code calls module_invoke with the following arguments: ‘jquerymenu’, ‘block’, ‘view’, 0 . What this does is call the jquermenu module, it calls its hook_block, tells it that it wants the information that is returned under $op = ‘view’ and that it wants only the information from the block with the $delta of 0.

Now that you have the $block (which is an array) you need to get the ‘content’ element from it by calling $block[‘content’]. You could also call the title by using $block[‘title’].

I hope that makes this code snippet make sense for you. You can find more information regarding module_invoke at http://api.drupal.org/api/function/module_invoke.