Web Development Institute is the premier institute in Delhi-NCR offering wide range of web design and web development courses. All our courses are designed by experts and in consultation with students from the advanced regions of the web world.

Read more

Our Web Development Courses are known for its professional excellence. Our highly qualified & experienced faculty offers intensified practical training to the students in the field of web development. They also render free career counseling.

Read more

Want to talk to our career advisor!! Feel free to contact on any of the following numbers:

Ring us at:   +91 9911-78-23-50
                      +91 9811-81-81-22

How to Develop a Module in Drupal?

How to Develop a Module in Drupal

It is true that developing a module in Drupal is not child's play but when it comes to an expert then it becos very easy. Although you must have basic knowledge of HTML, CSS, JavaScript, and PHP etc but here I will show you how you can develop a module in seconds.

Let's explore some important terms before going to study real Drupal module development.

how to develop a module in drupal

What are Hooks in Drupal?

Modules in Drupal almost entirely depends on Drupal Hooks. Drupal hooks are a method by which you can add your own actions to the default Drupal system.

You can read more about Drupal hooks and API here Drupal API Reference. In this tutorial I will use all the following given hooks:

  • hook_help()
  • hook_block_info()
  • hook_block_view()
  • hook_menu()
  • hook_permission()

Step by Step Guide to Develop a Module in Drupal

Let's start the real thing here. The first and important thing is to know the files thos are required to create a Drupal module. So, initially you need to create two files only to develop your first module. Suppose you are working on a module its name is 'admec', then you need to have following files.

1. admec.info   2. admec.module

admec.info

This is the file that tells or inform to the Drupal core about the admec module. The structure can be take like this.

name = Admec Module
description = This module displays recent contents in a block.
core = 7.x

Read more about the .info file here.

admec.module

This is the file that contains all the logics or programmings. Drupal provides approximate 8 types of Hooks and APIs to use and create advanced modules.

The following code uses a Drupal hook i.e. hook_block_info(). Remember you should not use the closing PHP tag i.e. '?>' when developing a module. In .module file you need to add block comments like the following because Drupal uses Doxygen to create the documentation from the given comments. This is the reason why Drupal uses strict commenting in module development.

I am developing a module using underlying code that will display a block in block page and later on we can display all the current posts in this. I will divide it in few steps for easy learning.

Module Development in Drupal: Step 1

Implementing hook_help()

Here we will use or first hook i.e. hook_help. All contributed modules must include it as per the guidelines from Drupal. hook_help provides help about the module to the user of the module. We will replace "hook" in the hook name with our module's name in Drupal, and now creating a function in the .module file with that name. So, the name of our function would be admec_help() in the admec.module file:

<?php
/** 
 ** Implements hook_help(). 
 ** Displays help and module information. 
 ** @param path  
 ** Which path of the site we're using to display help  
 ** @param arg Array that holds the current path as returned from arg() function  */ 
     function admec_help($path, $arg) { 
          switch ($path) { 
          case "admin/help#admec": 
             return t("Displays links to nodes created on the particular range of dates");
        break;
      } 
   }

Note: Do not use closing PHP brackets.
Now enable your module and check by entering this url http://example.com/admin/help. If nothing comes than clear all the cache and retry.

Module Development in Drupal: Step 2

Implementing hook_block_info()

I will be using hook_block_info() to define a block with no content later I will do that in future steps.

Copy these lines and paste just after the hook_help().

<?php
/**
 * Implements hook_block_info().
 */
function admec_block_info() {
  $blocks['current_posts'] = array(
    // The name that will appear in the block list.
    'info' => t('Current posts'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}
?>
Note: Do not use closing PHP brackets.
Just save both of the files and put under the sites->all->modules->admec folder (create a folder in modules folder and paste both of the files here). Now go to the modules and enable the module 'admec'. Then go to the blocks and have a look in disabled area. You will notice a block over there. Set this block in any of the reason and see the result.
 
You will not be seeing any post yet in that block. I will write the other lines of code soon.
 
Hope this article helped you. Please post your feedbacks in comments.
 
Thanks
 
Drupal related few more posts