Webemania | How to display a block programmatically in Drupal

| Published Date: 11 Dec 2013 | Last Updated Date: 04 Sep 2017
Total Reads: 20,453 | Last Visit: 22 Sep 2017 12:06:50 AM

This tutorial solves the following questions:

  • Placing the contents of a block in any location
  • Best way to programmatically embed the Block
  • Outputting a block programmatically in Drupal
  • How to insert a block programmatically with Drupal
  • Programmatically printing a block
  • How do I programmatically show a view block
  • Create your own custom Drupal block programmatically
  • Programatically output a webform block
  • Calling an instance of a already assign block. Suppose whose online block is already assigned in left sidebar. Now we want another instance of whose online to place in top

There are variety of ways we can create DRUPAL block. Welknown ways are:

  • Using custom/contributed module
  • Using built in system and
  • Using Views

Adding the contents of a block

Block generate by module

To add the contents of a block into a node, a custom block, custom module, or a page template, use the following syntax:

  1. <?php
  2. //D7
  3. $block = module_invoke('module_name', 'block_view', 'block_delta');
  4. print render($block['content']);
  5. ?>

  1. <?php
  2. //D6 and earlier
  3. $block = module_invoke('module_name', 'block', 'view', 'block_delta');
  4. print $block['content'];
  5. ?>

Example: Display who is a new user

  1. <?php
  2. //D7
  3. $block = module_invoke('user', 'block_view', '2');
  4. print render($block['content']);
  5. ?>

  1. <?php
  2. //D6 and earlier
  3. $block = module_invoke('user', 'block', 'view', 2);
  4. print $block['content'];
  5. ?>

Block generate by block

  1. <?php
  2. //D7
  3. $block = module_invoke('block', 'block_view', 'block_id');
  4. print render($block['content']);
  5. ?>

  1. <?php
  2. //D6 and earlier
  3. $block = module_invoke('block', 'block', 'view', 'block_id');
  4. print $block['content'];
  5. ?>

Example: Display who is a new user

  1. <?php
  2. //D7
  3. $block = module_invoke('block', 'block_view', '2');
  4. print render($block['content']);
  5. ?>

  1. <?php
  2. //D6 and earlier
  3. $block = module_invoke('block', 'block', 'view', '2');
  4. print $block['content'];
  5. ?>

Block generated by views with arguments

  1. <?php
  2. //D6 and earlier
  3. print views_embed_view($view_name, $display_id); // without argument
  4. print views_embed_view($view_name, $display_id, $argument); // with single argument
  5. print views_embed_view($view_name, $display_id, array($arg1,$arg2,$arg3)); // with multiple arguments
  6. ?>

How do i know block id (block delta)?

Go to block page (admin/build/block for drupal 6) or (admin/structure/block for drupal 7), hover your mouse on configure link, you will see a link in status bar. Last part is block id.

Determining drupal6 block id
figure 01: Built in Block create by user module
Determining drupal6 block id
figure 02: Custom block created by site admin (user)
Determining drupal6 block id
figure 03: Block created by view

Tags

Comments

http://recoverperu.org

Post new comment

The content of this field is kept private and will not be shown publicly.
Max length 128 characters
Image CAPTCHA
Enter the characters shown in the image.

About This Mania

Hasan Hafiz, Blogger at web mania ltd (www.webemania.com). He has more than 5 years experience in web development platform. He worked as a Sr. Software Developer at Canadian based outsourcing software firm, Lead Developer at countries top most VOIPswitch development company and Sr. Software Engineer at USA based outsourcing software firm . His expertise areas are PHP, MySQL, CSS, Javascript, JQuery, HTML, XML and MVC Framework Codeigniter. He has been along with Drupal from more than 4 years. You may call him Drupal Addicted.