18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2<br />

Creating the module directory<br />

Now that we know that our modules should go in /sites/default/modules, we<br />

can create a new module there.<br />

<strong>Module</strong>s can be organized in a variety of ways, but the best practice is to create a<br />

module directory in /sites/default/modules, and then place at least two files<br />

inside the directory: a .info (pronounced "dot-info") file and a .module ("dotmodule")<br />

file.<br />

The directory should be named with the machine-readable name of the module.<br />

Similarly, both the .info and .module files should use the machine-readable name.<br />

We are going to name our first module with the machine-readable name first,<br />

since it is our first module. Thus, we will create a new directory, /sites/default/<br />

modules/first, and then create a first.info file and a first.module file:<br />

Those are the only files we will need for our module.<br />

For permissions, make sure that your webserver can read both the .info and<br />

.module files. It should not be able to write to either file, though.<br />

In some sense, the only file absolutely necessary for a module is the<br />

.info file located at a proper place in the system. However, since<br />

the .info file simply provides information about the module, no<br />

interesting module can be built with just this file.<br />

Next, we will write the contents of the .info file.<br />

Writing the .info file<br />

The purpose of the .info file is to provide <strong>Drupal</strong> with information about a<br />

module—information such as the human-readable name, what other modules<br />

this module requires, and what code files this module provides.<br />

A .info file is a plain text file in a format similar to the standard INI configuration<br />

file. A directive in the .info file is composed of a name, and equal sign, and a value:<br />

name = value<br />

[ 29 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!