Tips on writing drupal modules

We recently launched our online GPL software CD store using the Drupal e-commerce plugin. Since all the supported payment gateways do not support ZAR we knew we would have to write our own. We integrate to payment gateways quiet often so I didn't think it would be much of an issue but the poor documentation for Drupal developers meant it took longer than it should have.

I thought I would record some of my learnings for others benefit. I would like to make this a more detailed tutorial but I just don't have the time. So here goes with some tips. Hopefully this helps some-one.

Tip 1: Drupal Framework Overview

The Drupal framework uses "hook" methods for allowing custom plugins to be called at the appropriate time by the application. These methods are defined by the framewokr and you implement them by creating a method for your plugin with the name <file_name>_<hookname>. Hook methods are like _help which will be called by the framework to display help information at the appropriate time. You would implement this in a image module by writing a function image_help(). So the first thing to do is find out the hook method for your module you are writing. There is some info on the Drupal web site but you will also find info on module specific site.

Tip 2: Most modules and module frameworks implement an API

Modules decide what custom methods to call on the user defined module by calling an general api method like _transaction api or _payment api. These methods have parameters the first of which contains the specific object for that api then $ops which will be used to determine which method to call and lastly two optional parameters $3 and $4. The $op variable is a string which is used in a switch statement to determine which custom method to call.

Tip 3: Drupal's database access techniques

Drupal operates on the database in three ways. One approach uses associative arrays where the field names are the keys and the values are the field values ($user["username"]).

The second uses an object approach with object properties being the same as field names and their values being the field values ($user->username) . What was confusing for me is that it seems to switch between the two randomly so you have to look at the code to know if you need to send in an array or an object.

Thirdly Drupal also uses direct access to the database with insert,select and delete statements. This approach should be avoided or at least contained in one method for talking to the database. It makes the code easier to maintain and more portable.

Tip 4: Extending the Drupal tables

Where possible use the frameworks defined methods to write and read info from the database. There are methods such as store_transaction_save. You can often extend the database with your own tables. You simply add your custom fields to your associative array or as properties to your object and the standard methods will automatically persist them to your table by calling a method you define in an api method. Typically the $ops parameter will be "load","save" or "delete".

Tip 5: How to enable/disable a module if you cannot access the site or admin panel

Some time Drupal will not start if there is an error in one of your modules or a dependency is not satisfied. To fix this you can either edit your code or, if the error is due to something else, you can enable or disable the relevant module by updating the status field in the system table for the relevant module.