Last updated 26 April 2016. Created on 5 January 2008.
Edited by madan879, Ayesh, aspilicious, jhodgdon. Log in to edit this page.

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.

Name your module

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. Important note: It is not just a convention that the short name is used for both the module's file name and as a function prefix. When you implement Drupal "hooks" (see later portions of tutorial), Drupal will only recognize your hook implementation functions if they have the same function name prefix as the name of the module file.

It's also important to make sure your module does not have the same short name as any theme you will be using on the site.

Create a folder and a module file

Given that our choice of short name is "onthisdate", start the 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 these 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.

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.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

IbnKhaldun’s picture

See the php sintax reference at http://docs.php.net/manual/es/language.basic-syntax.instruction-separati...

They say: " It' s convinient to omit the ' ?> ' close tag, in include() or required() files when you are planing to send http headers "

mkmk’s picture

It is not mentioned anywhere.

I wish this entire section was rephrased into:

Edit XXXXX to contain the following:
+++++++++++

>
+++++++++

bartl’s picture

I quote:

Create a PHP file and save it as onthisdate.module in the directory ...

Thus the name of the module file is "onthisdate.module".

alroca01’s picture

I'm following the instructions step by step because I need to create a module to embed the facebook button on my drupal website. I did it following this recommendations: http://www.propdrop.com/blog/facebook-button-drupal but when I try to create a php file with DW CS4 it doesn't work. May you share the correct code to put it in my php file, please? Thanks.

bartl’s picture

The reason for omitting it is because any whitespace after the "?>" (like a newline) would be sent as part of the HTML output. Omit the "?>" and no whitespace is sent.

donquixote’s picture

No more $Id$ ?
It seems that we no longer need the "// $Id$" at the beginning of php files.
Is this officially documented somewhere?

vegantriathlete’s picture

From what I understand that "tag" was necessary for CVS. Drupal.org is now using Git, which does not need (or use) the tag.