In this tutorial, you will learn how to create your first Drupal module. You are going to call this module starting drupal dev.

The first thing you need is create a folder called starting_drupal_dev, which will contain the module code.

Steps to create the module folder:

  1. Go to sites/all/modules
  2. Create a folder called contrib
  3. Create a folder called custom
  4. Create a folder inside “sites/all/custom” called “starting_drupal_dev”

Note: When you download a contrib module from drupal.org, you should put it in the contrib folder. Any custom modules that you develop should go into the custom folder.

As a minimum module needs just two files, a .info file and a .module file. The .info file tells Drupal important information about your module. The .module file contains the code. The info and module files must have the same name as the module itself.

To create your first module, follow these two steps:

  1. Create a file called “starting_drupal_dev.info” inside the starting_drupal_dev folder
  2. Create a file called “starting_drupal_dev.module” inside the starting_drupal_dev folder

Starting Drupal Dev module code

Info file

The .info file tells Drupal that the module exists. Without it, the module would be invisible to Drupal. It also provides information for the module admin page.

In starting_drupal_dev.info, add the following:


name = Starting Drupal Dev
description = My first module
core = 7.x

Name (required): Display name for the module. Names should be capitalised.
Description (required): Short description which will be displayed on the module list page. Limited to 255 characters.
Core (required): Version of Drupal this module is designed for. Drupal 7 is 7.x, Drupal 6 is 6.x and so on. You can not specify a minor version of Drupal (such as 7.9).

Module file

In starting_drupal_dev.module, add the opening PHP tag and comment for the file:


/**
* @file
* Module file for starting drupal dev
*/

The .module file is the heart of your module. In most modules, all of the logic is in the .module file. In some bigger modules, code may be in other included files (and declared in Files in the info file). To keep things simple, we will use the .module file for all of the code.

You have now created the required files for a working module! Of course, you will add more files as you go, but a .info and .module is the bare minimum.

Enable the module

Now let’s enable this module. You can enable the module in the module page (admin/modules), or if you are using Drush, you can run the following Drush command:


$ drush en starting_drupal_dev -y
 

Go to the modules page, and you will see that Starting Drupal Dev is enabled.

Is our module really alive?

At this stage, you have no real way of knowing that the module is alive and kicking. You can see that it is enabled on the modules page, but wouldn't it be nice to quickly add some code that actually returns something from the module? My favourite way is to implement a Drupal hook called hook_init(). This hook is called at the start of each page request as long the page is not cached. So if you are logged in, hook_init will get run because generally pages are not cached for logged in users.

Let's go ahead and add the implementation of hook_init() to test that the module is alive. In starting_drupal_dev.module, add the following function:


/**
* Implements of hook_init().
*/
function starting_drupal_dev_init() {
  drupal_set_message('Our module is alive!');
}

Now you need to clear the cache. You can do this by going to Configuration and Peformance and hitting Clear Cache, or you can run the following Drush command: $ drush cc all

Reload any page on the site and you should see the message "Our module is alive".

Note: drupal_set_message() is a built-in Drupal function. As the name suggests, it displays a message to the user.

Now that we have tested that our module is alive, you can remove the init function. You have just implemented your first hook!

This tutorial is part of the Starting Drupal Development course. If you would like to receive all of the lessons, please add your details in the box below.

To scale the Drupal development learning curve faster, check out my book Master Drupal Development. It will help you master module development faster to become a fully fledged Drupal developer.