CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Drupal 7 – Creating a module (Part – 1)

Drupal is the famous Content Management System used to build websites. Drupal allows extending its functionality by creating modules.

Drupal has its own Core modules and allows users to develop their own modules; say Contributed modules and link with them with Drupal.

In this series of articles, we are going to discuss creating modules in Drupal. Note that, these articles are based on the Drupal 7 Content Management System.

Path to store Drupal module files

Step 1. The first thing we have to do while creating a module is choose a proper meaningful name for it. Let us say “example” is the module name.

Step 2. Now we need to choose the location where to store these module files.

Drupal uses <Drupal Installed Location>/modules/ directory to store its core modules. Drupal recommends storing custom or contributed modules into <Drupal Installed Location>/sites/all/modules/ directory.

Step 3. Module files must be grouped into a directory name and the name of the directory must be the same as the name of the module name.

So, in our example, we are creating an “example” module. All its files must be in the “example” directory under <Drupal Installed Location>/sites/all/modules/ directory.

Drupal .info file

Step 4. The first file we are going to create for the “example” module is .info file. .info file provides module information eg: module name, description, files used for module et.,

So, our .info file name should be “example.info”.

Step 5. Now place the following text into the “example.info” file.

name = Example
description = "An Example Drupal 7 module for CodeSteps.com"
package = Drupal Development - CodeSteps.com
core = 7.x

Each .info file is defined with elements that describe or control the module.

  • name element is to display the name of the module.
  • The description element is the meaningful description of the module.
  • The package is to identify with which package the module is associated.
  • core element tells us based on what Drupal version the module is developed.

This time we have placed limited information into the .info file. We need to update this as we add functionality to our module.

Drupal .module file

Step 6. Another important file we need to create is the .module file. This is the file where we need to write the actual logic of our module.

So, the name of our module file must be “example.module”.

Now we have created “example.info” and “example.module” files. We have added some information to our “example.info” file and the “example.module” file is empty. Yes, we have not yet added anything to our module file “example.module”.

Step 7. Now open modules page in Drupal-based website. Observe that, our Example module will appear on the modules page.

Drupal 7 - Modules page with Example module information
Drupal 7 – Modules page with “Example” module information

It’s so simple, right? Let us add the functionality to our module in our upcoming articles.

**

Drupal 7 – Creating a module (Part – 1)

One thought on “Drupal 7 – Creating a module (Part – 1)

Leave a Reply

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

Scroll to top