Drupal Module Development
by Jennifer Dixey
What’s a Lullabot? (Why this Workshop?)
- Lullabot is Angie Byron (webchick), James Walker (walkah), Nate Haug (quicksketch), Jeff Robbins (jjeff), and Matt Westgate (matt westgate)
- Top Drupal contributors/developers (webchick, walkah, quicksketch all taught workshops)
- Well known trainers in Drupal technology (particularly walkah and webchick)
- Funny people with a dry and/or goofy sense of humor (makes a 5 day workshop much more fun)
- Wrote the book – the O’Reilly book, anyway – on Drupal "Using Drupal"
Drupal Module Development – The Basics
- A very basic Drupal Module is a .module file, plus a .info file
- The .module file defines the module’s functions (in PHP)
- The .info file makes a Drupal installation aware of the module
- Modules have to be “registered” with the Drupal install via the .info file
What We Learned: 1. Hooks and Forms
- The “hook” concept is how Drupal modules interact with core & contributed functions
- There is a “hook” for almost everything in Drupal’s functionality
- Hooks are named with easily understandable, English-like names, like:
- hook_perm (handles permissions)
- hook_form_alter (makes changes to forms
- hook_menu (handles menus and page display)
- Hooks don’t always do what you think they would, just based on the name!
- hook_menu, for instance, both creates menus and displays pages of content
- but once you know how to approach it, modifying a Drupal site makes sense
Hooks and Forms - Hands-On Project:
- “Captain Hook” module displayed a pirate picture if you chose “I am a pirate” on a simple form
- I needed to do something that felt more “hands on”, so I found a variety of pirate pictures on Flickr, and wrote a simple PHP program to rotate between them on page views (neato!)
What We Learned: 2. Menus and Pages
- Drupal hook_menu both displays menu options (i.e., tabs at the top of the screen, main menu choices) and generates pages when menu options are clicked
Menus and Pages - Hands-On Project:
- “Magic Menus” module made different menu options and tabs come up and generated different pages based on the menu choices
What We Learned: 3. Database Access
- Drupal uses MySQL, via a database abstraction layer
- Drupal v7 will enhance this capability so that the database abstraction layer can talk to different types of databases, not just MySQL, and this will be extensible so more types of databases can be used seamlessly with Drupal (just in time, since Oracle’s acquisition of MySQL via Sun puts into question the future of MySQL as an open source database mainstay)
- Everything is handled via a Drupal function called db_query, which passes SQL SELECT, UPDATE, DELETE, etc. queries through to the underlying database using the appropriate syntax
Database Access - Hands-On Project:
- “Databasics” module provided us with practice in doing database calls in Drupal modules, and combining that with forms, menus and pages
What We Learned: 4. Security
- Drupal’s model is (sort of) GICO … Garbage In, Clean Out … for efficiency’s sake. In other words, there is no automatic sanitizing of data on input. However, data is sanitized on output, or when a browser requests data. If you want to sanitize data on the way in, you need to add your own PHP to do so (but Drupal core provides ample ways to do this without writing a lot of code)
What We Learned: 5. Contributing Back to the Drupal Project
- Drupal is an open source project that gets 100% of its development from “volunteers”
- Some “volunteers” may be sponsored or otherwise paid to contribute, but the product remains open source, and is (usually) contributed back to the community
- The easiest way to contribute is to find a module (or even core code) that needs work, find a way to solve existing problems, and suggest them to the module/code maintainer(s)
- If your solution works, it may get applied to the module or section of core as a patch
- You can then apply for a CVS account on Drupal.org and propose patches directly
- Once you’re a contributor with a CVS account, you remain one, so you can keep contributing, start your own module projects, etc
- The Drupal community is huge; the contributor community is comparatively small
- Drupal is in great need of good-looking themes, so a big opportunity exists to earn instant contributor credibility, for those who can tackle creating excellent, CSS-compliant themes and making them work with Drupal seamlessly
- Another great way to contribute is to volunteer for the Docs team, as Drupal docs are spotty
There was no hands-on component to sections 4 and 5 of the course (they couldn’t very well have us hacking into Drupal servers willy nilly, could they?), but it was still very informative and useful. We are making heavy use of a contributed module that has no “maintainer” currently (so no one is looking after fixing bugs, etc.) so once I become proficient at module development, I hope to contribute patches, etc. and make it work even better and integrate more smoothly with other modules we’re using.
Page Updated
12.08.2009
