Drupal 7 Module Development

For PHP developers, this is the most practical book available if you want to start coding within Drupal. Every chapter has working code samples, allowing you to establish a great foundation for building your own tools.

Drupal 7 Module Development

Progressing

Matt Butcher et al.December 2010

For PHP developers, this is the most practical book available if you want to start coding within Drupal. Every chapter has working code samples, allowing you to establish a great foundation for building your own tools.

Who This Book Is For

If you are a PHP developer or a Drupal user looking to dive into Drupal development then you will find this book an excellent introduction to coding within Drupal.

Those with some Drupal experience will also find this an invaluable tool for updating their knowledge about the powerful new features of Drupal 7. Theme developers looking to extend their abilities will find this an accessible introduction to PHP coding within the Drupal environment.

This book assumes that you are familiar with basic PHP programming, along with HTML and CSS.No experience with programming Drupal is required, although it is also a handy way for experienced Drupal developers to get up to speed with Drupal 7.

Table of Contents

Chapter 1: Developing for Drupal 7

Introducing Drupal (for developers)

Drupal architecture

Drupal's major subsystems

Tools for developing Drupal code

Summary

Chapter 2: Creating Your First Module

Our goal: a module with a block

Creating a new module

Working with the Block API

Writing automated tests

Summary

Chapter 3: Drupal's Theme Layer

Business logic versus presentation logic

Data granularity

Theme engines

Two ways to theme

Render elements

The power of theme()

Theme registry

What else?

Summary

Chapter 4: Theming a Module

Reusing a default theme implementation

Steps to build a default theme implementation

Summary

Chapter 5: Building an Admin Interface

The User Warn module

Starting our module

The Drupal menu system

Form API

Sending mail with drupal_mail() and hook_mail()

The token system

Summary

Chapter 6: Working with Content

Why create your own entities

The goal

Bundles

The Schema API

Declaring our entity

Entity management

Summary

Chapter 7: Creating New Fields

Our goal: a "dimensions" field

How Field API works

Creating our new field type

Exposing fields to the Form API with widgets

Using formatters to display our field

Managing non-Field fields

Finding entities and fields

Summary

Chapter 8: Drupal Permissions and Security

Using user_access() to assert permissions

Checking the proper user account

Using hook_permission()

Declaring your own access functions

Responding when access is denied

Enabling permissions programmatically

Defining roles programmatically

Securing forms in Drupal

Handling AJAX callbacks securely

Summary

Chapter 9: Node Access

Node Access compared to user_access() and other permission checks

Using hook_node_access()

When to write a node access module

Creating the role access module

Modifying the behavior of other modules

Testing and debugging your module

Summary

Chapter 10: JavaScript in Drupal

JavaScript inside Drupal

Adding JavaScript

Altering JavaScript

Drupal specific JavaScript

AJAX helpers

Summary

Chapter 11: Working with Files and Images

The Twitpic and watermark modules

Files in Drupal

File API

Stream wrappers

Images in Drupal

Summary

Chapter 12: Installation Profiles

Introducing installation profiles

Drupal distributions

Creating profiles

Running the installer from the command line

Summary

What You Will Learn

Build modules extending Drupal's core functionality

Create custom content types and entities

Learn how to work with Drupal's hook system

Explore the major APIs introduced in Drupal 7 that the pros use to build successful Drupal websites

Apply the principals of Test Drive Development (TDD) using Drupal's built-in SimpleTest framework

Leverage the book's working module examples as a starting point for you own code

Discover the secrets of Drupal professionals as they explain the practical aspects of good Drupal development

In Detail

Drupal is an award-winning open-source Content Management System. It's a modular system, with an elegant hook-based architecture, and great code. Modules are plugins for Drupal that extend, build or enhance Drupal core functionality.

In Drupal 7 Module development book, six professional Drupal developers use a practical, example-based approach to introduce PHP developers to the powerful new Drupal 7 tools, APIs, and strategies for writing custom Drupal code.

These tools not only make management and maintenance of websites much easier, but they are also great fun to play around with and amazingly easy to use.

If you're eager to learn about these new APIs and start creating modules for Drupal 7, this is your book. Walk through the development of complete Drupal modules with this primer for PHP programmers.

From basic modules and themes to sophisticated Drupal extensions, learn how to use Drupal's API and hook system to build powerful custom Drupal websites. With an emphasis on practical programming, this book takes a project-based approach, providing working examples in every chapterSpecifically written for Drupal 7, this book will get you coding modules as quickly as possible, and help you add the features that will give your work that professional gloss!

This book will walk you through the development of complete Drupal modules and show you how to add various features to meet your requirements.

The Drupal content management system, written in the popular PHP language, has skyrocketed in popularity.

Developers for this system are in high demand. This book prepares PHP developers for Drupal development, explaining architecture, exploring APIs, and emphasizing practical approaches.

In each chapter, readers will learn new aspects of the system while creating fully-functioning modules, themes, and libraries. Learn how to “hook into” any part of the Drupal process, creating custom content types, extending existing capabilities, and integrating with external services and applications.

Authors

Matt Butcher

Matt is a web developer and author. He has previously written five other books for Packt, including two others on Drupal. He is a senior developer for the New York Times Company, where he works on ConsumerSearch.com, one of the most traffic-heavy Drupal sites in the world. He is the maintainer of multiple Drupal modules and also heads QueryPath – a jQuery-like PHP library. He blogs occasionally athttp://technosophos.com.

Larry Garfield

Larry Garfield has been a PHP developer for over a decade and a Drupal developer for half a decade. He is currently a Senior Architect and Consultant with Palantir.net, a leading Drupal development firm specializing in institutional non-profit clients. Larry is also the Drupal database maintainer and maintainer of many Drupal modules, and was one of the lead organizers of the GoPHP5 project. He writes about Drupal and, occasionally, other technical subjects at http://www.garfieldtech.com/

John Wilkins

John Wilkins has been a web developer for a long time. In April 1993, he was one of the lucky few to use the very first graphical web browser, Mosaic 1.0, and he’s been doing web development professionally since 1994. In 2005, John finally learned how idiotic it is to build your own web application framework and discovered the power of Drupal; he never looked back.

In the Drupal community, he is best known as JohnAlbin, one of the top 20 contributors to Drupal 7 and the maintainer of the Zen theme, which is a highly-documented, feature-rich “starter” theme with a powerfully flexible CSS framework. He has also written several front-end-oriented utility modules, such as the Menu Block module. John currently works with a bunch of really cool Drupal developers, designers, and themers at Palantir.net.

Matt Farina

Matt Farina is a senior Front End Developer and Engineer at Palantir.net, a Chicago-based firm that specializes in Drupal development. At Palantir.net, he works on a wide variety of projects ranging from museums to large interactive sites, including OpenSource.com. He is a contributor to Drupal code as well as a maintainer of multiple Drupal modules.

Prior to doing Drupal development full time, Matt worked in the defense industry for General Dynamics Land Systems. At General Dynamics he started as a Diagnostics and Testability Engineer. He eventually transitioned into the role of a Software Engineer building internal web based tools.

Ken Rickard

Ken Rickard is a senior programmer at Palantir.net, a Chicago-based firm specializing in developing Drupal web sites. He is a frequent contributor to the Drupal project and is the maintainer of the Domain Access, MySite and Menu Node API modules. At Palantir, he architects and builds large-scale web sites for a diverse range of customers, including Foreign Affairs magazine, NASCAR and the University of Chicago.

From 1998 through 2008, Ken worked in the newspaper industry, beginning his career managing web sites and later becoming a researcher and consultant for Morris DigitalWorks. At Morris, Ken helped launch BlufftonToday.com, the first newspaper web site launched on the Drupal platform. He later led the Drupal development team for SavannahNOW.com. He co-founded the Newspapers on Drupal group (http://groups.drupal.org/newspapers-on-drupal) and is a frequent advisor to the newspaper and publishing industries.

In 2008, Ken helped start the Knight Drupal Initiative, an open grant process for Drupal development, funded by the John L. and James S. Knight Foundation. He is also a member of the advisory board of PBS Engage, a Knight Foundation project to bring social media to the Public Broadcasting Service.

Prior to this book, Ken was a Technical Reviewer for Packt Publishing’s Drupal 6 Site Blueprints by Timi Ogunjobi.

Greg Dunlap

Greg Dunlap is a software engineer based in Stockholm, Sweden. Over the past 15 years Greg has been involved in a wide variety of projects including desktop database applications, kiosks, embedded software for pinball and slot machines, and websites in over a dozen programming languages. Greg has been heavily involved with Drupal for three years, and is the maintainer of the Deploy and Services modules as well as a frequent speaker at Drupal conferences. Greg is currently Principal Software Developer at NodeOne.

Several people played crucial roles in my development as a Drupal contributor, providing support and encouragement just when I needed it most. My deepest gratitude to Gary Love, Jeff Eaton, Boris Mann, Angie Byron and Ken Rickard for helping me kick it up a notch. Extra special thanks to the lovely Roya Naini for putting up with lost nights and weekends in the service of finishing my chapters.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.