Book Image

Learning Drupal 6 Module Development

Book Image

Learning Drupal 6 Module Development

Overview of this book

Table of Contents (14 chapters)
Learning Drupal 6 Module Development
About the Author
About the Reviewers


Drupal is a highly successful open-source Content Management System (CMS). It is well‑respected for its robustness, its flexible and immaculate code, and its seemingly infinite capacity for extension and customization.

Drupal 6, released early in 2008, represents a significant evolution in this already mature CMS. In this book, we build extensions for Drupal 6, focusing on the important APIs and libraries. We also highlight the new features introduced in version 6, making this book appropriate not only for those new to Drupal, but also those who are transitioning from version 5.

This book provides a practical, hands-on approach to developing Drupal modules. We also take a developer-centered look at themes and installation profiles—two other facets of Drupal that the developer should be familiar with. Each chapter focuses on the creation of a custom extension. Using this approach we develop a handful of modules, a theme, and an installation profile. However, more importantly, we get a practical perspective on how to make the most of Drupal.

By the end of the book, you will have a solid understanding of how to build modules for Drupal. With the knowledge of foundational APIs and libraries, you will be able to develop production‑quality code that fully exploits the power and potential of Drupal 6.

What This Book Covers

This book focuses on developing modules for Drupal 6. Each chapter introduces new concepts, libraries and APIs, while building on material from previous chapters.

Chapter 1 is a developer's introduction to Drupal. We take a look at Drupal's architecture, focusing on modules and themes. After covering some of the important concepts and taking a high-level look at foundational APIs and libraries, we finish up with a look at some useful development tools.

Chapter 2 gets us working on our first module. In this chapter, we develop a Drupal module that takes data from an XML feed and displays it as a block on our Drupal site. In this chapter, you will learn about the basics of module development, including what files need to be created and where they go. Hooks, a major component of Drupal development, are also introduced here.

Chapter 3 switches gears from modules to themes. Learning the theming system is integral to being able to produce high-quality modules. In this chapter, we create a theme using CSS, HTML, and PHPTemplates. We also take a look at the theme system architecture, along with some of the APIs.

Chapter 4 builds on the introduction to theming. In this chapter, we develop a new module that deals with a custom content type, a quote. This module uses the theming subsystem to prepare quotes for display. Our focus here is using the theme system to enrich modules. The theme API covered in this chapter is used throughout the rest of the book.

Chapter 5 focuses on Drupal's JavaScript libraries. Starting with the module we built in Chapter 4, we use the jQuery library and a couple of Drupal hooks to implement an AJAX (Asynchronous JavaScript and XML) service. This chapter also introduces the Database API and the menu system.

Chapter 6 is focused on building an administration module. This module provides an interface for administrators to send email messages to users. However, the Mail API is not the only thing we will look at. The tremendously important Forms API is also introduced here. We also get our first look at Drupal's access control features.

Chapter 7 takes a closer look at Drupal nodes. In this chapter, we use the Schema API, the Database API, and the node system to build a content type that represents a biography. Module installation files are introduced, and the Forms API and access control mechanisms are revisited.

Chapter 8 discusses filters, actions, and hooks—three of the more advanced features of Drupal. We create a module for emailing a newsletter to our users. We implement filters to prepare content for the email message. Actions and triggers are used to automatically send our newsletter when it is ready. Also, to allow other modules to interact with this one, we define our own custom hook that other modules can implement.

Chapter 9 changes tracks, focusing on installation profiles. In this chapter, we build an installation profile that can install a custom version of Drupal preloaded with the modules and themes of our choice. Working with the installer, we get a glimpse into Drupal's inner workings. Along with learning how to write code in this minimalistic pre-installation environment, we also look at registering themes and defining triggers automatically.

Who Is This Book For?

This book is written for PHP developers who want to add custom features to Drupal. You will need to know the basics of PHP and MySQL programming, but no experience of programming Drupal is required, although you will be expected to be familiar with the basic operation of Drupal.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "One thing that should stand out is the use of a require_once directive at the very beginning of the file."

A block of code will be set as follows:

function _philquotes_get_quote() {
$sql = "SELECT nid FROM {node} ".
"WHERE status=1 AND type='quote' ORDER BY RAND() LIMIT 1";
$res = db_query($sql);
$item = db_fetch_object($res);
// Do something with the $item.

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:

function philquotes_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Philosophical Quotes');
return $blocks;
case 'view':
$item = _philquotes_get_quote();
if(!empty($item)) {

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "Clicking the Add content type tab will load the form used to create our new content type."


Important notes appear in a box like this.


Tips and tricks appear like this.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to , making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on or email .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visit to directly download the example code.

The downloadable files contain instructions on how to use them.


Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from


You can contact us at if you are having a problem with some aspect of the book, and we will do our best to address it.