Developing Drupal Module

Hello, dude. How are you doing? We are planning to learn Drupal module development. Yes.. It’s true. Before start, I hope you have already completed previous tutorial. If not visit those first. Otherwise you may face some problem in here. Because we will discuss some pretty advance thoughts and code in this tutorial. And after learning Drupal module development, people look at you with some respect. So, get ready and sit tight.

Drupal Icon

What is Drupal Module?
Modules are pieces of software, which help you to accelerate your software building process. Drupal modules help to extend the power of Drupal. This kind of helpful pieces of software is also available for other frameworks and Content Management System (CMS). Like, in Zend framework, it is called Module. In WordPress, it is called plugin. In Joomla world, it is known as extension. Each module has a name that uniquely identifies it among all modules. You can download all of these Drupal modules from Drupal module repository.

Drupal Directory Structure
Let’s say, our project directory is w3pro. Inside this directory you will get two folders called modules and themes. Never touch these folders.

Drupal dir structure

One very popular proverb every Drupal developer believe –

Never Hack Core

Besides that sites folder is quite safe. All of your modules and themes will place into sites>all>modules or sites>all>themes. Infect, all the modules and themes you download from Drupal repository or other places, goes into these folders.

Drupal Module Naming Convention
Every module we will create in our life, we need to name it, in the first place. Drupal module has two types of names:

  1. A human-readable name: Humans read this name. It should be one or a couple of words long. The words should be capitalized and separated by spaces. For example, one of the most popular Drupal modules has the human-readable name Views. A less-popular Drupal 6 module has the human-readable name Eldorado Superfly.
  2. A machine-readable name: Drupal uses this name. It can be composed of lower-case and upper-case letters, digits, and the underscore character. No other characters are allowed. The machine names of the above two modules are views and eldorado_superfly, respectively.

By convention, the two names –

  • should be as similar as possible.
  • Spaces should be replaced by underscores.
  • Upper-case letters should generally be changed to lower-case.

Learn About Hook
Hook

Drupal’s hook system is the most important feature of Drupal programming. Most modules are nothing but a collection of hook implementations that Drupal automatically calls as it works its way through the request lifecycle. It’s not just the Drupal core that declares and calls hooks. Any module can declare and call its own hook.

hook life cycle

When Drupal checks to see if any module needs to be initialized, it looks to see if any modules implement a hook for initialization. It scans the loaded modules to see if any of them implement the function hook_init(). To implement a hook in Drupal, we need to declare a function that follows the hook-naming pattern. For example, a module called helloworld wants to implement hook_init(). It needs to declare a function named helloworld_init() (replacing the word hook with the name of the module).

Perhaps, two most used hooks are – hook_block_info() and hook_block_view(). If we implement these two hooks for our helloworld module, we need to implement its as follows –

/**
* Implements hook_block_info().
*/
function helloworld_block_info() {
     //your code
}

/**
* Implements hook_block_view().
*/
function helloworld_block_view($block_name = '') {
	// your code
}

In multiline comment block we need to say, which hook we are going to implement.

You will get an intensive list of hooks from https://api.drupal.org/api/drupal/includes!module.inc/group/hooks/7

Drupal t() function
Drupal supports dozens of languages. t() function is used to translate. Means, the t() function is responsible for translating strings from one language to other. This is one of the strongest features of Drupal’s internationalization and localization effort. The method by which Drupal supports translation is largely through the t() function.

You can get more detail about t() function from – https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/t/7

Our First Module, Hello World
By now you have enough theory in your pocket. Let’s create our first module. Like other programming language, we will create “Hello World” module. As you already know, all of our modules will go into sites>all>modules folder. So, create one folder called helloworld inside modules folder. Inside this folder, create two files called helloworld.info and helloworld.module.

helloworld module folder

So, now question is what are these two files? These are the two required files for a functional Drupal module.

  • The .info file: This is a configuration file that allows you to specify certain defined properties—some required and some optional, which provide information for Drupal to process/handle the custom module.
  • The .module file: This is the file that will have all of the PHP code for our custom module.

This is time to write code. Open helloworld.info with your text editor and write the following code.

name = Hello World
version = 0.1
description = A hello world module.
package = HelloWorld
core = 7.x

files[] = helloworld.module

;dependencies[] = autoload
;php = 5.2

Code explanation

Line 1, is for name of our module.

Line 2, is the version number.

Line 3, is the description string of our module.

Line 4, is optional package information.

Line 5, is minimum core Drupal requirement.

Line 7, is an array of module file name.

Line 9 and 10, are the dependencies and php version information, which are optional. But you may use these as your requirement.

Open helloworld.module file with your text editor and write the following code.

<?php

/**
* Implements hook_block_info().
*/
function helloworld_block_info() {
     $blocks = array();
     $blocks['helloworld_module'] = array(
       'info' => t('A hello world module'),
       'cache' => DRUPAL_NO_CACHE,
	);
    return $blocks;
}

/**
* Implements hook_block_view().
*/
function helloworld_block_view($block_name = '') {
	if ($block_name == 'helloworld_module') {
		$block = array(
		'subject' => t('Helloworld Module'),
		'content' => t('Hello!!! <em>Can you</em>.. believe that you are going to be a <strong>Drupal Module Developer</strong>.'),
         );
		return $block;
     }
}

/**
* Implements hook_help().
*/
function helloworld_help($path, $arg) {
	if ($path == 'admin/help#helloworld') {
       return t('This is a help information for our Hello World module.');
	}
}

Code Explanation

From line 3 to 13, we have implemented our hook_block_info() function. It contains the block information.

From line 15 to 26, we have implemented our hook_block_view() function. It contains the block content. It takes a parameter in $block_name when it is called.

Finally, from line 28 to 35, we have implemented our hook_help() function. This hook is used to create help information. It takes two parameters when it calls, $path, $arg.

We are almost done buddy. Now go to Modules from Drupal toolbar. You will see your Hello World module is already there. Enable it by saving configuration.

enabling helloworld module

You can click on the help link to see your help doc.

Now, go to Structure>Blocks and scroll down, you will see your hello world module block is already there. Set the Region as Sidebar second.

set block region

Scroll down and save blocks. Now go to the home page by clicking Home button. What are you seeing, amazing, right!!!

final page

Conclusion
This tutorial is the first step of the Drupal module development. Though it has less amount of code, you need to understand lots of Drupal philosophy. In next tutorial, I will demonstrate some advance module development features.

Happy coding… 🙂

Developing Drupal Module

Hej, I’m from Bangladesh. Learning programming is one of the freaking decisions I have taken in my life. Because, it makes me and my life crazy. I have great weakness on open source technologies. Perhaps, that’s why I do not know any closed source language. I fall in love with programming, when I started my undergraduate in East West University. Till now, I can not live without it.
Print Friendly
3 comments on “Developing Drupal Module

Leave a Reply

Your email address will not be published. Required fields are marked *