Getting Started

Last modified: June 10, 2009 - 15:07

As mentioned in the introduction, in this tutorial we're using the example of a module that lists links to content such as blog entries or forum discussions that were created recently, which we'll currently define as exactly one week ago. This page in the tutorial describes how to create the initial module file and directory.

The first step in creating a module is to choose a "short name" for it. This short name will be used in all file and function names in your module, so it must start with a letter and by Drupal convention it must contain only lower-case letters and underscores. For this example, we'll choose "onthisdate" as the short name.

Given this choice, start your module by creating a folder in your Drupal installation at the path: sites/all/modules/onthisdate. You may need to create the sites/all/modules directory first. Create a PHP file and save it as onthisdate.module in the directory sites/all/modules/onthisdate. As of Drupal 6.x, sites/all/modules is the preferred place for non-core modules (and sites/all/themes for non-core themes), since this places all site-specific files in the sites directory. This allows you to more easily update the core files and modules without erasing your customizations. Alternatively, if you have a multi-site Drupal installation and this module is for only one specific site, you can put it in sites/your-site-folder/modules.

Module files begin with the opening PHP tag followed immediately by a CVS ID tag in an inline comment. The CVS ID tag, $Id$, is a token which Drupal.org's version control system will fill out with revision information and authoring information. It is traditional to place this tag in your module regardless of whether you plan on putting it on Drupal.org or not. A similar ID tag is placed in all other files associated with your module.

<?php
// $Id$

The ID tag is not a special type of PHP syntax and is there purely for version control systems. Thus, it must be in an inline comment. Again, this tag will be filled out by automated processes and you can ignore it from here on out.

The module is not operational yet: it hasn't been activated. We'll activate the module later in the tutorial.

Coding Standards

As per the Coding standards, omit the closing ?> tag. Including the closing tag may cause strange runtime issues on certain server setups. (Note that the examples in the handbook will show the closing tag for formatting reasons only and you should not include it in your real code.)

All functions in your module that will be used by Drupal are named {modulename}_{hook}, where "hook" is a pre-defined function name suffix. Drupal will call these functions to get specific data, so having these well-defined names means Drupal knows where to look. We will come to hooks in a while.

 
 

Drupal is a registered trademark of Dries Buytaert.