Module Builder can significantly speed up your development of new and existing modules by generating fully formatted PHP code, 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 versatile Drush command, which writes the files you generate directly to their final location, ready to be enabled, or returns generated code to the shell.
- Automatically parses available hook names 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 api.drupal.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
How you install this module depends on whether it is to be used as a Drupal module or a Drush plugin.
If installed as a Drupal module, then the Drush commands will also be available on that site. However, if it's being mostly used with Drush then it's best to install it as a Drush plugin, and then get the benefit of it being available on all sites on a system.
Installation on Drupal
- Move this folder into your modules/ directory and enable it like you would any other module.
- Go to administer >> configure >> development >> module_builder and update settings for hook documentation files.
- Before the module can build anything it needs to retrieve hook documentation. Visit the "update hooks" tab and click button to generate it. When you want to update this documentation later on, return to the settings page and click the "Update" button.
- (Optional) Create custom function declaration template file(s) if you don't like the default output.
- (Optional) Create your own hook groupings if you don't like the default ones.
Installation on Drush
Install the module folder as a Drush plugin. One option is to put the entire module_builder folder inside ~/.drush See the Drush documentation for the other valid locations.
Before Module Builder can be used, hook data must be downloaded. Hooks are downloaded for core and any other module builder-aware modules that are in the active site. If Drush will be used with multiple Drupal installations, the hook data can be store centrally and used for all sites. The best way to do this is to specify a data directory in drushrc.php:
$options['data'] = '/PATH/TO/DIR';
If no data directory is specified then the files will be stored in active sites's 'files/hooks' directory.
To download the hook data, from anywhere in an active site codebase, do:
$ drush mbdl
This will put the API documentation files into the configured download folder for core and any other modules in this install of Drupal.
To download to a different location, (the target directory should already exist) do:
$ drush mbdl --data=/PATH/TO/DIR
You should then use the --data option for subsequent module builder commands such as 'mb' and 'mbdl'. 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.
The limitation of using the --data option is that only the 'mb' and mbdl' drush commands recognize the --data option.
Alternatively you can set a path using
drush vset module_builder_hooks_directory "/home/<ME>/.drush/hooks"
Using the Module
Using The Module on Drupal
- Click the new "module_builder" link in the menu at the top of the Modules page (note: you will require 'access module builder' privileges to see this link).
- Enter a module name, description, and so on.
- Select from one of the available hook groupings to automatically select hook choices for you, or expand the fieldsets and choose hooks individually.
- Click the "Submit" button and watch your module's code generated before your eyes! ;)
- 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.
- Start customizing it to your needs; most of the tedious work is already done for you! ;)
Using The Module on Drush
Once you have generated your hooks documentation files, you can also use the following drush commands:
List all available hooks and their descriptions. Through several questions, interactively generates hook code with description, help text, comments, dependencies, and package settings and returns formatted code to shell.
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.
Provided your hooks are documented in an api.php file, module builder will pick them up when hooks are scanned.
If you need to specify extra detail such as which files hooks should go into, 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.