Write your first Drupal 7 module

First steps in module building

There are two files needed when building modules:

  • a .info file which stores metadata about the module to notify Drupal of the existance of a module and renders its information on the administration pages
  • a .module file where the PHP code is stored

Both files should have the same filename and need to be in the same directory. In this example, the directory is called module_framework. In the directory, a module_framework.info and a module_framework.module file are available.

The .info file

The .info file can contain several properties, but only name, description and core are required. Modules can be grouped by package name so it is good practice to add a package name to your custom modules.

name = Module framework
description = Provides a simple framework to create a module.
package = Module examples
core = 7.x


When the .info file is created, the module is added to the module list.

The .module file

Before adding any drupal hooks or functions to the .module file, add a comment on top of the file to tell what it does. Also, never add a closing tag at the end of the file. From the Drupal coding standards:

  • Removing it eliminates the possibility for unwanted whitespace at the end of files which can cause "header already sent" errors, XHTML/XML validation issues, and other problems.
  • The closing delimiter at the end of a file is optional.
  • PHP.net itself removes the closing delimiter from the end of its files (example: prepend.inc), so this can be seen as a "best practice."

 * @file
 * A file that is used as a .module template file.
 * Every .module file needs this block of comment.
 * Never add a php closing tag to the file.