Developing a Module for Drupal - A Bird's Eye View

The learning curve for writing Drupal modules appears on first glance to be ENORMOUS. This year I've spent a lot of time trying to understand it a little better, and I'd like to share my general process with any new Drupal developers who are finding module writing a little intimidating.

  1. Create a list of requirements. This should be the first step in ANY application development, but it often gets overlooked. Think about what you immediately need the module to be able to do and future uses the module might have. Separate out the immediate requirements but don't ignore the future possibilities. You'll want to make sure that your data structure and application can later stretch to encorporate those enhancements.
  2. Look at existing modules that are similar to the one you are thinking of developing or that have functionality similar to what you would like to create. For example, I looked at how the e-commerce module was organized when I first started developing my library module. Also, the create/edit library action pages were heavily based on the role pages from the Drupal user module. If there is already a module that does nearly what you want, perhaps you would be better served to contact the module maintainer for that module to see if the functionality you need can be incorporated.
  3. Based on the requirements and your initial module research, determine if you'll need more than one module. For example, with the library module, I needed to separate out a module for the patron content type. (Tip: if you need a custom content type for your module, that's one sub-module all by itself.)
  4. Decide on a name for your module, and create a folder with that name.
  5. Create a .info file. All files in the module directory should be prefixed with the module name as should all the functions within the module.
  6. Create a data structure for your module. All the other functionality will depend on it. Make sure you keep your naming consistent. Look at the tables other modules create for ideas. If your module is really simple, you may not need additional database tables and can skip the rest of the next step.
  7. Once you have a structure, create a .install file. As of Drupal 6, adding a data structure for your module is much simpler. Just use the library_schema() function to define your tables and columns. Look at other modules for syntax examples.
  8. Create a .module file. This is where most of the main functionality will be located.
  9. Create a list of all the 'pages' in your site, then define each one as an item using hook_menu. The _menu function is somewhat like an outline of the module.
  10. If you have a really large module, create a few appropriately named additional module files. (e.g. .admin.inc and .theme.inc). Make sure you go back to the _menu function and update the file locations if a given page function is in a file other than the .module file.
  11. Overwrite any other Drupal hooks that you will need. Some examples: _perm (to define additional user privelages), _nodeapi (to append additional functions to existing node types), _form_alter (to make changes to existing forms).
  12. Become good friends with http://api.drupal.org
  13. A nice addition to a module is an uninstall function. You'll also find it really useful if you make big changes to your module during development.
  14. Use the coder module. It's fantastic.
  15. When testing the new module, try using the devel module to make sure you don't have a lot of repetitive SQL calls. However, make sure to test without it as well. I found that it added so much additional load to the site that some of my pages did not work when it was enabled, and I spent a number of days looking for a non-existent bug. *sigh