How To Programmatically insert block in Drupal Template ?


In Drupal, the boxes which can be made to appear in various regions of a Drupal website are called Blocks. This functionality is provided by the Block module, which is a part of Drupal 7 core. They are an extensible core feature of Drupal with a simple API provided by the block module. The Block Administration page at administer ? site building ? blocks lists all blocks in draggable table rows so you can just drag them between regions. Each block can be configured with conditions for its visibility (such as which user roles will see it) and on which pages it will appear (using path-matching). You can also use PHP code for more complex conditions.

There are many ways by which you can create Drupal block ,commonly are:

  1. By using Built in system module.
  2. By using custom/contrib module.
  3. By using Views

You can assign these block to any region by using interface for region assign in "admin/structure/block" by configuring particular block. Here we are going to show you how you can assign block programmatically. For this perpose you can use function module_invoke() For block created by system module

<?php //D7 $block = module_invoke('block', 'block_view', 'block_id'); print render($block['content']); ?>

<?php //D6 and earlier $block = module_invoke('block', 'block', 'view', 'block_id'); print $block['content']; ?>

For block created by contrib/custom module

<?php //D7 $block = module_invoke('module_name', 'block_view', 'block_id'); print render($block['content']); ?>

<?php //D6 and earlier $block = module_invoke('module_name', 'block', 'view', 'block_id'); print $block['content']; ?>

How to get module name and block id


block p1 image

Add new comment