one inch frame
the personal site of eric a. Farris

Drupal 7 Module Development

Drupal 7 was released on 5 January 2011. The latest version of the venerable Content Management Framework (or System, if you wish) contains hundreds of changes, including a new paint job, new standard features, and lots of work under the hood. Drupal is one of those projects that is not afraid to break backwards compatibility for the sake of a more elegant and powerful system. As such, each new release of Drupal brings with it a need for new documentation. It is in this spirit that Drupal 7 Module Development from Packt Publishing was born. This book is meant to help programmers familiar with the PHP programming language to learn how to develop custom modules for Drupal 7. Packt Publishing was kind enough to send me a review copy.

The first thing I noticed about the book were its authors. There are no less than six authors for this book, and the names are ones that the Drupal community will know. This, along with the foreword by webchick herself, gives the book instant credibility to those within the community, and the list of the authors’ credits at the beginning of the book should lend an air of authority to those without. It does not disappoint.

The book’s almost 400 pages are divided up into twelve chapters, two appendices, and an index. Along the way, several small custom modules will be built, along with custom Drupal entities, fields, and even an installation profile. But the book starts at the beginning.

Thankfully, though, not the very beginning. I suspect half of the books on Perl that sit in my library start out with where to get it and how to compile it. Many Drupal books start off similarly, telling you to go to drupal.org and grab the tarball, set up a (L|W|M|X)AMP stack, and so forth. As this book is aimed at developers, not admins, we are spared those steps. Chapter One is instead a fantastic introduction to what is known as “The Drupal Way(tm)” for developers already familiar with PHP. It is an excellent introduction to Drupal 7 for developers. If I were going to get a team of programmers started with Drupal development, I’d use Ch1 as the introduction, especially if they’ve never used Drupal before. Including mentions of the devel module, coder module, and drush shows that the authors are serious about using the best tools for the job. Using the testing subsystem from the beginning, our first module in Chapter 2, is also a very good sign. Included is a rundown of Drupal’s amazing coding standards. Something that first drew me to Drupal almost ten years ago is the cleanliness of the source code, and that’s thanks to the coding standards and the ways it is enforced. The book does a good, though not perfect, job of sticking to these standards throughout the book, and that’s to be commended. Among the standards it attempts to adhere to are the Doxygen comments. Not too surprising, given the authors, but this feels like very modern Drupal development. Absent, however, is any real discussion on development environments, though I suspect here we’re assuming developers are already comfortable with something.

Chapter three could quickly lose you at first reading. It’s a dense discussion of the theme system, which at first might seem out of place in a developer’s text. If you thought Drupal 6’s theme system was an onion, wait until you see the layers in Drupal 7’s Theme API. The examples are fine, but to me there are more details here than necessary. We’ll be told several times to “see the API site for more details” later in the book, and some of that glossing over would be welcome here.

The main problem with Chapter three is that it’s all just a setup for Chapter 4, sort of an “I told you that story to tell you this one” deal. We’ll theme our module, using some, but no where near all, of the layers we learned about in Chapter 3. They perhaps could have been thinned and combined, but it’s still nicely done.

Chapter four contains my favorite quote in the book, talking about making an API instead of burying your logic in hooks:

Remember, be lazy. If your module’s API is good enough, some other developer will write the code to integrate your module with theirs.

So we’re a third of the way through the book before “it” rears it’s ugly head. In chapter five we see it — the Form API, Drupal’s most complex subsystem. The introduction to it is a good one, building an administration form, one of the more simpler uses for FormAPI. It’s an excellent way to start dealing with all the nuance and power in the arrays. Sitting in my rocking chair with my pipe, pausing to yell at the kids on my lawn, I could tell you of days before there was a FormAPI, and there’s little doubt that this is better. Still, it’s complicated, but the authors here do a good job of getting you into it. We’ve seen and have been warned about Drupal’s love for the structured array all through the first few chapters, and here’s where it really hits hard. Again, the examples are clear and worthwhile, and do delve into the majority of what a developer will need to know.

On the other hand, Chapter six deals with something completely new to Drupal 7, the ability to create “entities” that are not nodes. While there is no doubt this is an incredibly powerful new subsystem, allowing you to leverage Drupal for all sorts of things which can turn Drupal into “Anything” Management instead of “Content” Management, it seems to me that we’re going down this road just because we can. That’s necessary in a book concerned solely with development, I suppose. Perhaps I’ve just got old-Drupal-guy syndrome, but I would have liked a good explanation why we’re building our own entities instead of overloading the node system, something we do in Drupal 6 all the time.

I think I would have liked better the treatment of Chapter 7 before 6. In chapter seven, we’ll go through hanging fields off of our entities. This could have used the node system for this, and then we could have talked about creating new entities that inherit this ‘fieldability.’ In seven, we’ll even create our own field types, and, while it’s implied that nodes can take advantage of this, I fear that new developers will see chapters six and seven as The Way things are done, instead of the new entities being presented as a way to better allow Drupal 7 to fit in places where Drupal 6 needed some shoehorning.

Philosophy aside, these continue to be well-written, with useful examples, and nice treatments of leveraging Drupal’s built-in functionality to create robust types of content with custom code. In contrast to the “why do this?” feeling I had in Chapter 6, Chapter 7 presents a very clear reason why we’re diving into creating our own field type instead of repurposing existing types for our cause. I would have loved to see the same reasoning given for not using a node as the basis for our custom data types in the previous chapter.

While I appreciate the effort taken to define some viewing queries for our new data types, Chapter 7 should, I think, instead go into programmatically creating views via the Views API. Yes, this is a “third-party” module, but in name only. No Drupal site worthy of the name will run without views.module within the last three years or so. It’s a good time to talk about it, but it just slips by without a mention.

Chapters 8 and 9 are examples of very fine, clear, and concise technical writing. A good overview of the permissions system, great introduction to the new and updated hooks, and well-done examples of where to deviate from the easy and standard to the harder and custom. Among the highlights of chapter 8 is a discussion of what FormAPI does to increase the security of Drupal forms. In nine we get to the node access system, and it’s dealt with through useful examples, good discussion of what’s happening behind the scenes, and excellent points to consider when leveraging this powerful, and potentially tricky, API.

The low point of the book for me was Chapter 10. It is a glossing over of the Javascript and AJAX available in Drupal. While a full treatment of the subject is quite literally a book all its own, this chapter may make too many assumptions about what a PHP programmer might know about JS/AJAX, or even what to do with it. I would have liked this chapter to be fleshed out quite a bit more. I found the examples given to be silly and without any practicality. For example, in keeping with the “Artwork” content type examples used earlier, it would have been interesting to add a bit of ajax validation to the form for our “dimensions” field, alerting us when we have, for example, left our field without all the dimensions defined. Such browser-side validations are good UI and some examples here would have been quite helpful, both in continuing what earlier chapters started, and in showing real-world uses for the powerful subsystems we’re talking about. Alas, no treatment of the subject exists.

In Chapter 11 the FileAPI and ImageAPI systems are given a light workout, showing small but useful examples of how to leverage the systems that before were relegated to contributed modules. We even get into a tiny bit of GD programming as we add a watermark to images. Again, it’s light, and perhaps could have been fleshed out some more, though the examples given are good.

The final chapter of the book deals with installation profiles. While at first it may seem that would be a discussion best left for site and system administrators, there are actually quite a few places where we can add code to customize the installation, and it’s nicely done in the example profile.

The first appendix deals with the new database subsystem, as Drupal now relies on a heavily wrapped PDO. It’s obvious, to a fault I think, that this information was part of Chapter six and was later spun off into the appendix. There is a fair bit of good information in here on writing queries the new way, and it’s most welcome. I would have liked to see the appendix and Chapter six switch roles — a whole chapter on the new db stuff, and an appendix on the new entity system.

Appendix B is little more than a pamphlet on web programming security in general and Drupal programming security in particular. It’s a nice but very sparse treatment on a big and important topic. Again, this is a topic that literally has its own book, but still I wanted more.

What the book gets right

The answer to that is almost everything. This book will serve as a tremendously helpful introduction to Drupal programming for programmers. It does assume you know PHP, and warns you (or comforts you, depending) early on that there won’t be much OOP here and there will be a lot of structured arrays. The chapters don’t necessarily follow in order one to the next, and aren’t explicitly linked one with another.

For programmers new to Drupal chapter one should be required reading. And while occasionally the book slips into “what’s new” mode, for the most part the reader is considered a competent PHP coder but not necessarily someone fluent in Drupal programming.

The examples given throughout are mostly useful. One thing I hate about programming texts is when they show what the language/system can do with ridiculous examples. With the glaring exception of the Javascript chapter, this book does not stoop to such levels, instead giving real-world problems solved through the Drupal API.

The style of the book is nicely conversational. The overt humor is sparse, but at the same time you don’t ever feel that you’re sitting in a lecture hall. The authors seem quite well-versed in the material and aren’t subjecting you to death-by-powerpoint-by-screenshot. They don’t talk down to you, ever, guiding the reader through the API as a comrade. Even in the areas of security and best practice, when it would be easy to slip into browbeat mode, they resist, presenting facts instead of opinion and advice rather than orders.

What the book gets wrong

The main problem I had with this book is that I feel there are parts missing. The book begins talking about how we’ll use modern techniques, and mentions Drush, the Drupal shell, one of my favorite tools. And that’s the last we’ll here of it. Seriously. Even when the installation profile chapter says we can use profiles at the command line, we end up writing a command line PHP script instead of leveraging Drush. An oversight in the editing, perhaps, to mention it at all?

Also absent any real treatment is contributing rather than simply developing. A “10 minute git tutorial” appendix wouldn’t have been out of line. There’s no mention of how to be a good module maintainer, dealing with releases, submitting your module to drupal.org, or any such thing. While that’s not necessarily within scope, such a discussion is noticeable in its absence, particularly when the community is perhaps the strongest thing about Drupal. Given the status of the authors as top-tier contributors, it’s a curious omission.

There are some nitpicks, some spelling errors, wrong screenshots, and discussions of things (ie., drush) that just aren’t there. No more, perhaps, than any other book of its size, and nothing that was too glaring or anything that would stop one in their tracks. We do occasionally slide away from coding standards (eg, after singing the praises of Doxygen comments earlier in the book, chapter seven contains no code comments at all), but it’s nothing too big.

Final thoughts

I was very happy to read through this book. It’s well written by competent authors. It gets me excited to write some custom code of my own for Drupal 7. While there’s not too much that will serve as a desk reference within the book (the authors are quick to point us to api.drupal.org, early and often), Drupal 7 Module Development is a worthy addition to the shelf and serves as a wonderful introduction to coding for Drupal.

Previously on one inch frame…

1 year 35 weeks ago
1 year 36 weeks ago
2 years 6 weeks ago
2 years 16 weeks ago
2 years 16 weeks ago
2 years 19 weeks ago
2 years 22 weeks ago
2 years 23 weeks ago
2 years 24 weeks ago
2 years 25 weeks ago
2 years 27 weeks ago
2 years 27 weeks ago

Activity Stream

-->
 
Forex Online | Hey! domain registration easy | High-Quality Hosting