[Drupal] How to retrieve the content of a Drupal block
There may arise situations when you would want to display the contents of a block inside a template file or along with any other output variables. module_invoke() function can be used for this purpose.
The module_invoke() is a function to invoke a hook in Drupal. The syntax for module_invoke is different for Drupal 6 and Drupal 7.
In Drupal 6, the syntax is: $block = module_invoke('modulename', 'hookname', 'op', 'id/delta'); In Drupal 7, the syntax is: $block = module_invoke('modulename', 'hookname', 'id/delta');
In order to get the contents of a block, we should invoke the hook_block() by passing the value 'block' as the second argument in the module_invoke() function call. If we want to get a menu, we will need to invoke hook_menu(), and for that use 'menu' instead of 'block'.
Let us have a brief look at the arguments to be passed.
- modulename : name of the module in which the required block is created.
- hookname : name of the hook to invoke.
- op : denotes the information to be retrieved or the action to be performed (list, configure, save, view in case of blocks).
- id/delta : id/delta/name (block's delta in case of retrieving a block).
Example:
Suppose we have a module name test inside which we have created a block named 'custom' ,we can render the block as below :
drupal_render(module_invoke('test', 'block_view', 'custom')) // D7 version drupal_render(module_invoke('test', 'block', 'view', 'custom')) // D6 version