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

Tips: 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:

| Read more..

Courtesy : Pixelclever.com