Creating a basic Drupal 7 module

Drupal Version
7

Ceating a basic Drupal 7 module

When looking to achieve something in Drupal you'll often find the best solution is to put some code into a custom module. This often sounds daunting if you're not very experienced in Drupal or PHP but it's actually very simple to create a basic module to hold your custom code. Although modules often contain many files, at its most basic level, a Drupal module only needs two files to function:

      YOUR_MODULE_NAME.info
      YOUR_MODULE_NAME.module

To create your basic module you'll first create a folder to contain your files. The name of the folder must be the name of your module and, of course, to avoid any conflict issues this name should not be the same as any existing Drupal module. For this example we'll call our module "my_custom_code", referred to as a module's 'namespace'.

NOTE: Your module should contain only alpha-number, lowercase letters and underscores.

STEP 1

Create a new folder called "my_custom_code"

STEP 2

Create two empty text files within your new folder called "my_custom_code.info" and 'my_custom_code.module'

The .info file will contain the basic information describing your module to Drupal and the .module file will contain the actual module code (PHP).

Drupal Custom Module Example

STEP 3

Open your my_custom_code.info file in a text editor and add the following information to it and save the file.

name = My Custom Module
description = Module to contain custom code for my website
core = 7.x

The 'name' and 'description' fields can contain what ever text you want. Make sure it is relevant and descriptive for your module. The 'core' field is the indicator to tell Drupal what version your module is for.

STEP 4

Open your my_custom_code.module in a text editor like Notepad++ and place the following PHP opening tag at the very beginning of the file:

<?php

NOTE: It is advised to not include the PHP end tag in the document. It is recommended to include the opening tag only to prevent possible issues.

Now you can place your code into your new .module file and save it. Of course, remember to enable the module in Drupal's module administration section.

An example of a Drupal hook that is frequently used is HOOK_form_alter() - whenever you use a Drupal hook in your own module you'll replace the word HOOK with the namespace of your module:

<?phpfunction my_custom_module_form_alter(&$form, &$form_state, $form_id){      drupal_set_message($form_id);  // print form ID to messages     drupal_set_message(print_r($form, TRUE));  // print array to messages}?>

NOTE: Sometimes you may not see the affect of the code you've put into the module. Sometimes you may need to clear Drupal's cache to refresh things and allow your module's code to take affect.

This is a demonstration on the most basic type of module. A Drupal module can be much more complex. More in-depth information and instructions can be found here on Drupal.org.

Author Information

Written by: Shawn Ostermann

Shawn is a Drupal Specialists with over 12 years of experiencing building and developing Drupal websites including custom module development and e-commerce websites.