Beginning Drupal Module Development: Custom Drupal Blocks Tutorial

By using drupal blocks you can easily add content to sections of your site from the administration pages. However, these basic blocks can only show static content.

This drupal blocks tutorial will walk through the basics of creating a custom module and creating a block in code that displays dynamic content. This tutorial is great for beginners, as we will walk through the basics of Drupal module development.

Create Custom Module

In your modules directory (sites/all/modules) create a folder for your module. For this example our module will be called myblockmodule, so we would create the directory sites/all/modules/myblockmodule. Your module can be called whatever you like, just make sure that when following this tutorial your replace “myblockmodule” with your modules name.

1. Create a .info file

All modules in Drupal require a .info file to let Drupal know that our module exists, what it’s name is, as well as other information about our module. In your module folder (sites/all/modules/myblockmodule), create a file named myblockmodule.info. In myblockmodule.info enter the following:

 

name = MyBlockModule 
description = This module provides a custom block with dynamic content. 
package = MyModules 
core = 7.x

 

The Explanation

This is how the above breaks down:

  • name is the human readable name of your module. This is required for all modules, It is shown on the modules administration page.
  • description is a short message explaining what this module does. This is also visible on the modules administration page.
  • core describes which version of Drupal this module works with. This is required and should be “7.x” for all Drupal 7 modules.
  • package is optional, but I recommend it. The main purpose of this is to define what category to list the module in on the module admin page.

There are other variables you can define in this file, but these are the main four I put in all modules I create. For more information on module .info files check out Writing module.info files.

2. Create a .module file

There are two files which every module must have. First is the .info file, which we already created. The second is the .module file. Despite the file’s extension it is really a PHP script. This file is evaluated every time Drupal a Drupal page is load. The main purpose of this file is to define functions which implement hooks. Hooks are evaluated during different Drupal events and can be called by Drupal itself or other custom modules. (For further reading on hooks: Hooks 101)

For our module we will create a new file in our module’s directory (sites/all/modules/myblockmodule) called myblockmodule.module. This file will implement two hooks, one to let Drupal know that we are defining a new block, and one to let Drupal know what the block’s content is. The file will contain the following:

<?php
/**
 * Declare what blocks are provided by this module.
 * Implements hook_block_info().
 */
function myblockmodule_block_info(){
    $block['marty_countdown'] = array(
        'info' => t('Marty Countdown'),
        'cache' => DRUPAL_NO_CACHE,
    );
    return $block;
}

/**
 * Define what our block is going to look like.
 * Implements hook_block_view().
 */
function myblockmodule_block_view($block_key){
    $block = array();

    if($block_key == 'marty_countdown'){ //We only want to define the content of OUR block
        //This is the title of the block.
        $block['subject'] = t('Marty McFly Countdown');

        //Define the block content.
        $block['content'] = t('Marty will arrive in ').(strtotime("2015-10-21")-time()).t(' seconds.');
    }

    return $block;
}

The Explanation

The first function defined above, myblockmodule_block_info() implements hook_block_info() It lets Drupal know about our block t by returning an associative array, whose values describe the blocks. In our example, we are only defining one block, keyed by  marty_countdown. The info variable defines what the block will be called in the admin backend. The cache variable defines if and how the block should be cached. In our case we do not want the blocked to be cached because it is showing real time information. Other caching options as well other ways to further define how your block should behave can be found on Drupal’s hook_block_info api docs.

The second function defined above implements hook_block_view() and is called whenever Drupal is trying to display a block to the visitor. The $block_key (referred to as $delta in the documentation) that is passed in lets us know what block Drupal is trying to display. We only want to tell Drupal what to display when it is trying to show our block, every other time we should just return an empty array. When the $block_key is ours, we should return an array with information on what to display. More specifically, subject, which is the default block title, and the content which is either a renderable array of the content or a string of markup.

Conclusion

Remember to enable the custom module and assign the new block to a visible region. Defining a block in code can be a quick way to include dynamic content on your site.

Advertisements

One thought on “Beginning Drupal Module Development: Custom Drupal Blocks Tutorial

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

WordPress.com.

Up ↑

%d bloggers like this: