How to Create a Basic Drupal Block Module

Drupal modules are somewhat like plugins. They add new functionality to a basic Drupal site. There are also already hundreds of modules available on the internet and several ongoing developments.

Hence, there might be a time that you may feel the need to create your own. In this article, we will create a basic block module. And with this, you can start creating your first module. Custom module is being saved to sites/all/modules. We will name our module as “simpledisplay”. Save it to sites/all/modules.

We will create two files. The first one is for .info which will hold your modules name and other description information. The second one is for  the .module which will contains codes for your module functions.

If you like this article you might be interested in some of our older articles on Exciting In CSS3, The Benefits of QR Codes, Making Magento Commerce Bend to Your Will with Faster Data Imports, The Branding Potential of the Internet Browser, and A/B Testing.

Creation of .info

Create a simple in your simple display folder. You may do so by using a notepad or any text editors. Copy the codes below (I am using Drupal 6 since some modules are not yet upgraded to Drupal 7):

name = "Simple Display"
description = "Simple Display is my first Drupal module."
core = 6.x
php = 5.2.9 The above details are what will appear in your site building/module sections.

See screenshot below:

Drupal Modules

Creation of .module

A module can  create available for a block or a content page, with or without admin. Here, we will only create a basic module available for a block. In the next tutorials, we will discuss a module available for a content page and with an admin configuration.

To create a module available for a block, you need to understand hook_block() function. It tells Drupal to install or deploy a block for a module. Add the codes below to your .module file and save your file as simpledisplay.module.

 * Implementation of hook_block()
function simpledisplay_block($op='list', $delta=0, $edit=array()) {
 switch ($op) {
 case 'list':
 $blocks[0]['info'] = t('Empty Details');
 return $blocks;
 case 'view':
 $blocks['subject'] = t('Simple Display');
 $blocks['content'] = t('Your contents here');
 return $blocks;

.module Code Explanation

There are three parameters in Drupal’s hook_block(), the $op, $delta and $edit.

  1. $op is to define operations in Drupal hook_block(). There are four operations available for this:  list, view, save and configure.
  2. $delta is to define your block in its list operation further. It is usually in numerical figure like 0,1,2,3 and so on. Since we are only dealing with only one block here, we defined our delta as 0. 
  3. $edit is use for the save operation. 

If you will observe, contents described within $blocks[ ] are illustrate within ( ) function. With or without t( ) function, your string will still appear on your block hence this function is to allow parsing the contents for multiple language translations.

You are now ready to install your first module. You may tweak details and add more functionality in the $blocks[‘contents’] if you want to display more.

Sharing is Caring
  • Elias

    Thanks for this tutorial. I wanted to quickly find a way to create a block from my module and your tutorial went right to the point.That’s exactly what I needed.

  • Joseph

    I haven’t ever taken time to try out Drupal but from just going through this article, I heavily know someone will in a broader way find it help (…it’s not that I didn’t find it helpful myself, I really did).

  • Nikhil Sharma

    This is a great tutorial for newbies on how to create a module block in Drupal.
    Thanks for sharing!