Creating blocks through modules using hook_block_info

Time and again we come across requirements where we need to create a block through modules with custom functionality like a custom slider. So, in this tutorial, I will show you how to use hook_block_info and a few other hooks to create a custom block.

So, today we are going to create a module which will have some custom code and the output of the custom code will be available as a block in the admin block section and we can place it anywhere we want to.

To create a block using modules we need to use two views i.e. hook_block_info which will hold the information required for the block and the second one is hook_block_view. This will have the functionality code.

We will try to create two blocks; one will show a normal “Hello World!” and the second will show the details of the current user i.e. His email address, his account created data. So, let’s dive in.

Here is my code for hook_block_info:

function demoblock_block_info() {
  $blocks['helloworld'] = array(
    'info' => t('Demoblock: Hello world'),
    'cache' => DRUPAL_CACHE_GLOBAL
  );
  
  return $blocks;
}

Here we define two things:

  1. The name which will be visible in the admin section (info part)
  2. The cache part of the block: This defines how to handle the cache of the block? Should it be done with the global cache handling mechanism or something else.

Once this is done, we can see the block visible in the admin section of the block. Check the screenshot given below:

Custom widget

To show data inside the block, we have to use hook_block_view. Here is the code:

function demoblock_block_view($delta = '') {
  $block = array();
  
  switch ($delta) {
    case 'helloworld':
      $block['subject'] = t('Hello World!');
      $block['content'] = _demoblock_helloworld_content();
      break;
      
  }
  return $block;
}
  
function _demoblock_helloworld_content() {
  return 'Hello World!';
}

Here switch case is being used to handle all the blocks which are defined in hook_block_info. If some of you have used the Drupal 6 functionality, then you will be aware of this. Anyways, switch case $delta is the blocks which are defined in the module.

The first is the “helloworld” block. We have defined the subject which becomes the title of the block and the content where I have passed a function. We can also use the hook_theme functionality which I will show you for the next block.

So, once this is done, we can assign a region to the block through the admin section and we would be able to see the block rendering as it should. Here is my screenshot of the block on the second sidebar:

Custom widget

Now that we have the first block in place. Next work on the second block which will be a little more complex than what we have created.

In this, I will use a theme function and display a few data coming from the global $user variable.

First, we add our block info in hook_block_info. So the final code becomes something like this:

function demoblock_block_info() {
  $blocks['helloworld'] = array(
    'info' => t('Demoblock: Hello world'),
    'cache' => DRUPAL_CACHE_GLOBAL
  );
  $blocks['userdetails'] = array(
    'info' => t('Demoblock: User details'),
    'cache' => DRUPAL_NO_CACHE
  );
  
  return $blocks;
}

The only change that you will see is the second line where I have mentioned that this block should not get cached because this will be different for every user. Again this might not be the best way to handle the thing.

Next, we define hook_theme so that we can use a tpl to present the data.

function demoblock_theme() {
  return array(
    'userinfo' => array(
      'template' => 'template/userinfo',
    )
  );
}

Once we have done that, we need to define the content source as theme tpl inside the hook_block_view like we have done for the first block. Here is the modified code for hook_block_view:

function demoblock_block_view($delta = '') {
  $block = array();
  
  switch ($delta) {
    case 'helloworld':
      $block['subject'] = t('Hello World!');
      $block['content'] = _demoblock_helloworld_content();
      break;
      
    case 'userdetails':
      global $user;
      $block['subject'] = t('Your profile');
      $block['content'] = theme('userinfo',array(
        'username' => $user->name,
        'email' => $user->mail,
        'timezone' => $user->timezone,
      ));
      break;
  }
  return $block;
}

Once this is done, we just need to present the data coming in the tpl. Well, you guys can do something better, I came up with this:

Welcome <strong><?php print $username; ?></strong>

<strong>Your email:</strong><?php print $email; ?>
 
<strong>Timezone that you are following:</strong> 

<?php print $timezone; ?>

Here is what I finally got for the user details block and the hello world block:

Custom widget

For reference, you can get the code from Github