Drupal 7 Module Development

This book review was published by Slashdot, .

Drupal 7 Module Development

While it is possible to create a simple website using a base installation of Drupal, the real power of this content management system is achieved through the use of modules, which can be thought of as add-ons that extend the capabilities of Drupal in specific ways — oftentimes in conjunction with other modules. These modules are developed and contributed by PHP programmers who understand how to use one or more of the Drupal application programming interfaces (APIs) to access information stored in a Drupal database, such as content, user profiles, and theme settings. These APIs have changed with Drupal version 7, and thus Drupal coders could benefit from a book that explains how to create Drupal 7 contrib modules.

One such resource, titled Drupal 7 Module Development, was made available by Packt Publishing on 3 December 2010, under the ISBN 978-1849511162. It has half a dozen authors, all of whom are highly experienced Drupal programmers and contributors to this burgeoning open source project: Matt Butcher, Greg Dunlap, Matt Farina, Larry Garfield, Ken Rickard, and John Albin Wilkins. This team effort spans 420 pages, organized into a dozen chapters, and two appendices. Angie Byron, the Release Manager for Drupal 7, starts it off with an interesting and upbeat foreword that concisely summarizes the primary goals of this latest release of Drupal. Following it is a preface whose chapter summaries are poorly written — almost as though the writer knew nothing about Drupal. Speaking of technical knowledge, readers are expected to be familiar with Drupal, PHP, HTML, and CSS — the more so, the better. For the jQuery material, an understanding of that library and JavaScript itself would be valuable. Packt Publishing hosts a book Web page that offers a detailed description of the book, links for purchasing the print and electronic copies of the book (or the two combined, for a large discount), and the example source code for nine of the chapters (also available from the book's GitHub repositories). As with all of its other titles, the chapters end with summaries, which provide no value and simply waste space.

This book's first chapter, "Developing for Drupal 7", provides an overview of the purpose of custom modules, the practical approach that the authors will take in explaining how to create such modules, the Web technologies underpinning Drupal, the Drupal architecture, its major subsystems, and various tools commonly used for Drupal programming. Oddly, the authors hope "that the code mentioned in this chapter can serve as a foundation for your bigger and better applications", and yet no code per se is mentioned. Nonetheless, the chapter does serve as a decent introduction for PHP programmers new to "the Drupal way". The second chapter, "Creating Your First Module", shows the reader how to do exactly that, using a very simple module to illustrate the basics, such as the files that typically compose a Drupal module, as well as some Drupal coding standards. Unfortunately, regarding the code on page 38 that checks whether the $path variable is "admin/help#first", no explanation is provided as to why the "#" is not a "/", given that the URL path in the reader's browser will be "/admin/help/first", and the "#first" does not refer to a page anchor. After a worthwhile detour into Drupal internationalization and the t() function, the authors introduce both the Block API and the Testing module, through example. Incidentally, readers trying out the sample code will want to add "static" to "public function getInfo()" in first.test — as is done in all of the other Drupal 7 core test files — to avoid a PHP "strict" warning of a static call to getInfo() in simpletest_result_form().

The next two chapters focus on theming — specifically, Drupal's theme layer and techniques for theming a custom module. The material in both chapters is arguably comprehensive, and thus ideal for a reader already well-versed in Drupal 6 module development. But, for anyone else, it will likely be overwhelming in its dense detail and in the fast pace at which it is presented — and thus will discourage most newcomers. The former chapter presents numerous high-level concepts, while the latter is intended to illustrate those ideas by focusing on module theming. Yet even if the reader carefully examines and implements the sample code — usually the best way to learn any sort of programming — these chapters will probably prove quite difficult for readers to comprehend thoroughly, unless they have prior experience along these lines. Oddly, the Chapter 4 summary tells the reader that she should have "learned a little bit about contributing your experiences [sic] and knowledge back to the Drupal community", but the material does not explain how to do so. (More on that topic later.)

Chapter 5, "Building an Admin Interface", provides a detailed survey of the Drupal menu system, the Form API (including how form data is saved, which is inadequately covered by some other books), Drupal's built-in email system, and the use of tokens therein. The coverage is again detailed, and would be even better had the remaining commonly-used HTML form elements — such as list boxes and radio buttons — been shown in the example code. For those readers whose heads are still spinning from the previous two chapters, this material may be a welcome change, in that the explanations are slower paced, with seemingly greater attention given to whether the Drupal newbie will be able to learn what is being taught, step by step. However, any reader who is using this chapter as a reference when creating a custom implementation of hook_menu(), will doubtlessly become frustrated by the inadequate advice on determining the valid possibilities to be used in the access arguments array: "[check] the hook_perm() implementation of the module in question". But what module? The reader is presumably creating a new one from scratch, with no permissions already set; so the authors must be referring to an existing module — but which one? If the reader were to search through all of the core and example modules, he would find no hook_perm() functions. Do the authors mean hook_permission()? This illustrates how critical it is for authors and technical editors of books purportedly for beginners, to strive to put themselves in the shoes of the poor reader, who does not possess their knowledge and experience.

As with any CMS, "content is king" for sites built using Drupal. Thus it is critical for Drupal module developers to know how to dynamically create and manage all of the elements required by a module working with content: node, entities, fields, etc. In earlier versions of Drupal, the familiar "node" concept did not encompass all of the non-node data types, such as users and comments — forcing developers to create workarounds in their modules and in their sites as a whole. Version 7 introduces "entities" and "bundles" (which can be thought of as sub-entities), to allow greater flexibility for programmers. Chapters 6 and 7 delve into these concepts, with plenty of example code and explanations thereof. Readers learn how to create database tables indirectly using the Schema API, define new entities, give users the capability to manage them, encapsulate multiple database operations into transactions, and define new field types, widgets, and formatters.

Chapter 8, which focuses on how to set and use permissions within modules, is straightforward, and includes sections on the secure use of regular form processing, as well as AJAX callbacks (for interactive form behavior that avoids the necessity of reloading the current page). Incidentally, there appears to be an error in the code on page 221: "function example_menu() example_menu() {". Chapter 9 continues in the same realm of security, specifically, use of the Node Access system within Drupal. The coverage is quite thorough, and the only problem is that some of the "tips" blocks repeat information found in the regular text. The chapter concludes with some valuable advice on how to test and debug node access modules, which can be especially difficult.

The last three chapters of the book cover some interesting and worthwhile topics: JavaScript, file management, and installation profiles. Readers learn how to add JavaScript and CSS to a site, how to use the Drupal Library API, and related matters. Sadly, readers may be perplexed by the numerous poorly-constructed sentences — especially near the beginning Chapter 10 — such as "JavaScript within a group and within the sub-group of being or not being included in every page are ordered by weight" (page 291), which sounds like a joint effort by Hamlet and the IRS publications department. The next chapter demonstrates how to use the new files and images API that was introduced in Drupal 7, and which allows developers to reduce the number of contrib modules required for building even the most basic website. Confusingly, the reader is told that, when installing Drupal (presumably version 7), he will probably see three error messages resulting from missing sites/default/files directories; but I certainly did not see this when installing any of the beta or release candidate versions, nor heard this from anyone else. The authors also explain stream wrappers, the Image API, and image styles (and the effects they can utilize). The last chapter shows how to set up custom Drupal profiles, their tasks, and the distributions that can make use of them. The reader is told that "input formats" are now referred to as "text filters", but Drupal 7 appears to have standardized on the term "text formats". The book's two appendices discuss Drupal 7's improved database layer, and security techniques applicable to all versions.

Packt's website states that there are no known errata, so one can only assume that the publisher's editors failed to spot many obvious flaws: "a[n] introduction" (page 1), "eXtensible" (page 10), "However, Not" (page 16), "it's own data" (page 17), "though means" (page 18), "architecture advanced" (page 25; it presumably should read "advanced architecture"), "( a", (page 28), "an[d] equal sign" (29), "the the" (41 and 146), "exercising [of] every" (49), "test[,] absolutely" (53), "a child element[s]" (78), "its" (84; should read "their"), "short-coming" (85), "then" (90 and 98; should read "than"), "you will be passed to" (108), "lets" (120 and 129; should read "let's"), "FormsAPI" (235), "to post spam [to] the site" (254), "ever[y] page" (291), "html" (311; twice), "is to" (318; should read "to"), "how to we" (326), "let's make create" (326), "is [a] result" (365), and "many [of] types" (376). The reviewers section lacks page numbers, but does not lack errors: ", (", "and [a] bug", and "including[,] reviewing".

The writing quality varies from chapter to chapter, and some passages are awkwardly phrased and confusing, such as "each of these two lines were split on to one line" (page 57). Scattered throughout the book, one will find cases of semicolons used where dashes are called for, commas where semicolons are called for, title case used when inappropriate or missing when appropriate (the book's preface is a veritable minefield), compound adjectives missing hyphens, adjectives incorrectly tied to nouns using hyphens, needed commas missing (Appendix A has some egregious examples), and the term "was" used where the subjunctive "were" is called for — in other words, the usual grammatical flaws found in books written by techies. Fortunately, the material is livened up with a few welcome bits of humor, and not the overreaching kind found in many programming books. Even more admirable is the attention to internationalization, unit testing, and other good practices.

The example code within the text may be intimidating to those new to Drupal, but it really helps demonstrate the concepts discussed in the text. The downloadable source code is helpful for avoiding retyping that code from the text, but needs to be cleaned up. For instance, the code for Chapter 4 is in a directory named "1162_04_All code", which suggests that it contains all three versions, but it does not. The code for Chapter 5 is split between a subdirectory named "old", which contains the newest code, and in another directory, "1162_05", which contains older code. The directory "1162_07_Code" contains no fewer than six different (and possibly differing) copies of its example module. How can the reader know which is the correct copy to use for following the book's discussion? Moreover, for some of the chapters, such as 5, the source code listed and discussed in the book does not fully match that provided in the downloadable archive file.

Overall, this book is a substantial contribution to the Drupal literature, but it is weakened by two obvious problems: Firstly, it lacks a chapter or appendix to explain how the reader could contribute a newly-created module to the Drupal community — specifically, Drupal.org's Modules section. This is a glaring omission, particularly in light of the (laudable) encouragement to the reader to participate in the community, as well as the authors' many contributions to the same. Secondly, because this book is supposedly suitable for Drupal beginners, and given the complexity of Drupal's APIs and their code requirements, the authors should have presented the concepts in more digestible chunks, at a slower pace, so as to be easily comprehended by someone new to Drupal programming using APIs. This is especially true of the second and third chapters.

The aforementioned problems could be corrected in a subsequent edition, which would be well worth the effort: Drupal 7 Module Development is an information-packed and wide-ranging resource for experienced Drupal programmers who want to enhance their existing module-building skills, and transfer them to version 7.

Copyright © 2010 Michael J. Ross. All rights reserved.
bad bots block