Building Modules with Drupal 6

I’ve learned quite a bit about how Drupal works with styles and how pages are rendered. Now I’m looking at how to build modules. What I would like to do is create a few custom modules that can “suck in” and combine information from other sources into a master feed – a feed aggregator on steroids. Similar to a “mashup” server of some kind. This is still a little fuzzy in my mind, but my intent is to look at ways to process and combine data streams from various sources into a “master view”.

As an example if I’m interested in the Saab Turbo-X then I would want to combine data feeds from enthusiast sources, Saab, GM, and a number of other places into a “master view” so that on one page I would have everything that I need to know about this car.

So with this in mind I embark on my trip down “module lane”. I can already see there are a few bends in the road, but before we go there, drupal.org has a great tutorial on module development so you should read this. There’s also a good section on setting up a development environment. For my development environment I’m running on Windows XP Pro, Apache 2.2.8, MySQL 5.x, PHP 5.2.5, and Drupal 6.2. For my actual development tool I am using the very latest Aptana Studio Community Edition – my current rev is 1.1.6.009905.

You should focus on the links above to start – as with my previous articles on Drupal I am not seeking to replace their excellent documentation, but really provide a “newbie” spin on how to get going. I’m not a PHP expert (but I sure am getting there…) and I would not call myself a Drupal guru either.

Using Drupal’s tutorial I was able to easily create a module and get it working, but I did have a few issues:

  1. The “.module” thing without a “.php” ending needs to get configured in your IDE too as a PHP file.
  2. There are some subtle differences in the .info files that can cause confusion with earlier versions of drupal, for example the dependencies key/value is now an array type. To add multiple dependencies you need to add these to the array one at a time.
  3. It is not entirely clear how to enable IDEs to recognize core Drupal libraries and functions so that “intellisense” lookup stuff works. Check this out for Aptana.
  4. CVS stinks…I use SVN…so I naturally don’t like posting anything to Drupal’s code managemnet system. You can get a decent CVS GUI client from tigris.org (the same people that make svn and tortoise).

I learned a few things that are very important when building modules:

  1. When you are building modules it is very important to not put any HTML formatting directly in the code. Use CSS tags and DIVs or your users will have fits trying to get things working especially if you are creating block type modules.
  2. Think of all the possible variables in your module and build an administration interface to make your module as flexible as possible.
  3. If things are not showing up and you can’t see any cause, disable and re-enable your module. Clear your cache. Turn off all caching and performance stuff in the administration interface.
  4. PHP (not really Druapl per se) has some interesting operators like “.=” that are used quite a bit in code and examples. You need to learn these or at least have a handy cheat sheet. “.=” is roughly equivalent to an “append” function in other languages
  5. Drupal then adds confusion a bit with some functions like “t()” and “l()“. These are important to know and understand, but they really should have decent names. Once you are up to speed on these it is ok.

Building modules in Drupal is pretty simple I think. What it takes is a decent IDE (aptana), a good understanding of PHP syntax, and some time studying the Drupal APIs. I’m going to work on a real world example in my next modules post. I’m also going to look at CCK and Views and examine if I can use these instead of a module.

6 Comments

  1. Plutarch

    Check out Triplify – might be of some interest for what you’re looking to do. I’ve just recently integrated it into my WordPress blog. It automatically generates RDF+N3 metadata for various resources on your PHP-enabled site.

    Here’s an example: http://ramandwhiskey.com/triplify/post/10 (View Source).

    Triplify: http://triplify.org/Overview

    I’m writing up something soon about my experience/ideas about Triplify.

    Reply
  2. berenerchamion (Post author)

    Hey thanks – I’ll have to check that out.

    I’m going to work a bit on the modules, then CCK/Views to see what I can do. Have you seen myplay.com – wow – that’s a drupal app. That’s some sweet stuff I think.

    I see there’s a bunch of RDF stuff for Drupal as well.

    beren

    Reply
  3. zefcan

    Great tutorial post! I’m starting to get into drupal module development as well and you’ve provided some good links.

    Cheers!

    Reply
  4. Dazza

    I did not spot that a Drupal php module does not have a php extension in the Creating modules tutorial on the Drupal site.

    Your comments above help me get started.

    Cheers!

    Reply
  5. Ambasta

    I call bs on this… to learn how to develop a simple world module, you have to read through pages of API and documentation to see how you can achieve it.. when all you need is a hook_menu with a function on MENU_CALLBACK and simply create a function that returns “Hello World”.

    Why go through all the abstraction phases before getting your hands on code.

    Instead they want to go through drupal concepts and then show you how to code. That would be acceptable too if they actually explained all concepts together, instead a partially convoluted module w/o theme or JS support is allowed.

    Just bloody irritating.

    Reply
    1. Beren (Post author)

      What are you calling “bs” on? I would agree that Drupal development – like anything else that’s worth doing – is not a simple task to learn and get working right. I laugh at people who say “hey I just whipped out an iPhone app (or whatever) with no effort at all”. Lol – you mean you made a piece of crap…same with Drupal…committ…learn…do…or as Yoda says “do not – there is no try”.

      beren

      Reply

Leave a Reply