Module Builder - A module building helper

Last modified: October 24, 2009 - 13:54

Module builder can significantly speed up your development of new and existing modules by generating your .info and .module files, complete with template functions for hook implementations.

It can be installed as regular Drupal module, and provides an admin interface to let you pick the hooks you want.

It also provides a Drush command, which writes the files you generate directly to their final location, ready to be enabled.

Features

  • Automatically parses available hook names from CVS at your command... so help keep those files updated, won't you? ;)
  • Comes with some sample standard hook function declarations to get you started and a default module header; but if you don't like those, simply rename the .template files to -custom.template instead and create your own definitions!
  • Saves you the trouble of looking at drupaldocs.org 50 times a day to remember what arguments and what order different hooks use. Score one for laziness! ;)
  • Automatically selects hooks based on your needs.
  • Option allows you to turn off informative comments.

Installation and Configuration: Drupal

  1. Move this folder into your modules/ directory like you would any other module.
  2. Enable it from administer >> modules.
  3. Go to administer >> settings >> module_builder and specify the path to save the hook documentation files
  4. The first time you visit the module builder form, the module will retrieve hook documentation from cvs.drupal.org and store it locally. When you want to update this documentation later on, return to the settings page and click the "Update" button.
  5. (Optional) Create custom function declaration template file(s) if you don't like the default output.
  6. (Optional) Create your own hook groupings if you don't like the default ones.

Installation and Configuration: Drush

Move this folder to somewhere where Drush can find the command. The prefered location is in a folder called .drush your HOME folder (i.e. ~/.drush/module_builder). Inside drush/commands will also work, though then you have to watch you don't clobber it on upgrade. Drush documentation has details on other options.

From a Drupal installation, do:

$ drush mbdl

This will download hook files to Drupal's files/hooks folder. Hooks are downloaded for core and any other module builder-aware modules that are in this install of Drupal.

To download do a different location, do:

$ drush mbdl --data=/home/ME/DIR

The directory should already exist.

You should then use the --data option for subsequent module builder commands. This means you use a central store of hook data for all your Drupal sites. Whatever hook files are already there will be overwritten, but this way you can accumulate hook files for modules present in only some of your installations. The hook data is processed each time files are downloaded, so all hook files are included.

Using The Module on Drupal

  1. Click the "module_builder" link in the menu (note: you will require 'access module builder' privileges to see this link).
  2. Enter a module name, description, and so on.
  3. Select from one of the available hook groupings to automatically select hook choices for you, or expand the fieldsets and choose hooks individually.
  4. Click the "Submit" button and watch your module's code generated before your eyes! ;)
  5. Copy and paste the code into a files called .module, .info and .install and save them to a directory under one of the modules directories.
  6. Start customizing it to your needs; most of the tedious work is already done for you! ;)

Drush commands

Example commands:

  drush hooks

List all available hooks and their descriptions.

  drush mb my_module menu cron nodeapi

Generate module code with hook_menu, hook_cron, hook_nodeapi. This is the simplest way to run module builder. --build=code is assumed.

  drush mb my_module --name="My module" --dep="forum views" --build=info

Generate an info file with readable name and dependencies.

  drush mb my_module cron --name="My module" --dep="forum views"  --write

Generate both module and info files, write files to sites/all/modules and also output to terminal. The --write option assumes you want --build=both. Add --quiet if you don't want terminal output. Add --yes to clobber existing files without being asked.

  drush mb my_module nodeapi form_alter --add

Append hooks to the existing module file.

  drush dochooks my_module

Add PHPDoc headers to all hooks (that don't have any) in the given module.

Telling module builder about your hooks

Lots of contrib modules provide hooks. You can tell module builder about them, so users can easily generate code for your module's hooks in their own modules.

To do this, implement hook_module_builder_info() in a file named YOUR_MODULE.module_builder.inc file in your module folder. See the contents of module_builder.module_builder.inc for a detailed example.

 
 

Drupal is a registered trademark of Dries Buytaert.