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.

Features

  1. Automatically parses available hook names at your command... so help keep those files updated, won't you? ;)
  2. 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!
  3. 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! ;)
  4. Automatically selects hooks based on your needs.
  5. 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

  1. Move this folder into your modules/ directory and enable it like you would any other module.
  2. Go to administer >> configure >> development >> module_builder and update settings for hook documentation files.
  3. 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.
  4. (Optional) Create custom function declaration template file(s) if you don't like the default output.
  5. (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 stored 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 '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 Module Builder in the Drupal backend

  1. Click the new "module_builder" link in the menu at the top of the Modules page ("admin/modules") (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! ;)

Using Module Builder with Drush

Once you have generated your hooks documentation files, you can use various drush commands to create modules.

mb-list

  drush mb-list 

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.

mb

  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 --write 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 --write --name="My module" --dep="forum views" --build=info

Generate an info file with readable name and dependencies.

  drush mb my_module nodeapi form_alter --add --write

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.

Comments

mikenewski’s picture

I followed the directions of:
Installation and Configuration: Drupal
I installed the module, went to the settings page and left the default values;
Path to hook documentation directory: hooks
verified the folder had been created in the directory:
sites/default/files/hooks
then per step 4 went to the admin/build/module_builder and got this error message:

"No hooks were found. Please check the documentation path specified in the Administer >> Site configuration >> Module builder page."

I tried the update hooks button: admin/settings/module_builder/update and it resulted in a blank page.

Could someone tell me what I've done wrong?

Thanks.

thinkinghuman’s picture

Same with me

electronicmonkey’s picture

Same here. Did you ever fix the problem ?
No hooks were found. Please check the documentation path specified in the Administer >> Site configuration >> Module builder page

The download tab does fetch a bunch of hooks documentation though. Don't know why nothing else works.

nilashis’s picture

Trying doing "drush mbdl" first

solution@vijay’s picture

I follow following steps and now I solved this problem.
I used simple method not drush. Just download module and place it into module directory. and then
1. Enable the module.
2. Go to configuration->module builder->go on update hooks tab->update.
After update you can see the following message.
" Your last hook documentation update was Tue, 08/26/2014 - 06:24.
You have the following hook definition files saved in public://hooks: "
3. Go to module menu -> . Here you can see the one more tab module builder ->. click on this tab.
Now you can use module builder module.

chrisphotonic’s picture

Tried to use the automatic download function, but it only saves files as 404 errors. That's why others are having issues.

hedinfaok’s picture

Just to let people with the same problem know ... (this page was the first hit in my search)

I installed module_builder in my drush/commands/ directory in a Drupal 6.4 (I know, I know ... upgrade ...) installation. I kept getting the following error when running drush mb-download:

Invalid argument supplied for foreach() module_builder.drush.inc:670 

... and I would get the following error when running mb-build:

Invalid argument supplied for foreach() process.inc:22                                                                                                        [warning]
PHP Fatal error:  Unsupported operand types in /opt/drush/commands/module_builder/includes/generate.inc on line 390
Drush command terminated abnormally due to an unrecoverable error.

I trashed the drush/commands/module_builder/ directory and installed module_builder into sites/all/modules/ and enabled the module. Both errors when away.

dman’s picture

When run successfully, the builder doesn't actually tell you *where* it created your new files. Documentation says it should probably be sites/all/modules/ but if you already have a folder called sites/all/modules/custom/ - it will be created under there instead.

I imagine it follows the same rules as drush dl for multisites (if you already have sites/sitename/modules and are running drush in {sitename} context, things go there instead of sites/all/)

ZenDoodles’s picture

This is not the place to report problems or ask for support. If you are having trouble, you're much more likely to get help in the module builder issue queue. Your question may have already been answered there, and even if it is not, module maintainers don't generally see comments on documentation pages.