Generating a custom module codebase

During a project, it is very likely that we will have to create one or more custom modules. This, at the very least, involves creating a directory at sites/all/modules/custom/module_name, plus the module_name.info and module_name.module files within that directory with some hook implementations. Module Builder provides a very powerful Drush command to generate all the boilerplate code that we need to start working with our module.

Note

Unfortunately there is no Windows support for Module Builder.

We will create a module that implements the hooks hook_menu(), hook_block(), hook_theme(), and hook_permission(). The command mb-build launches an interactive process to gather all the information needed to create the module. Here is an example, where we download and install it and then create our custom module:

$ drush pm-download module_builder
Project module_builder (7.x-2.x-dev) downloaded to [success] /home/juampy/projects/drupal/sites/all/modules/contrib/module_builder.
$ drush pm-enable --yes module_builder
The following extensions will be enabled: module_builder
Do you really want to continue? (y/n): y
module_builder was enabled successfully. [ok]
$drush mb-build --write
Enter the module name: sample_module
Enter the required hook presets:
Enter the required hooks: menu permission theme block
Enter the human readable name: Sample Module
Enter the description: Just a sample module
Enter the help text: This module serves as a simple example of module code generation
Enter the dependencies:
Enter the package:
Proposed sample_module.module:
<?php
/**
* @file sample_module.module
* TODO: Enter file description here.
*/
/**
* Implements hook_help().
*/
function sample_module_help($path, $arg) {
switch ($path) {
case 'admin/help#sample_module':
...
$ ls sites/all/modules/sample_module
sample_module.info sample_module.module

The previous command, after entering the required information, created the module directory structure for us (the --write option is needed for this) and printed the contents of each file onscreen. Now, if we check out the contents of sites/all/modules/sample_module/sample_module.module, we will see that it has added a very clean and descriptive syntax for each of the hooks that we specified, including suggestions of what our next steps could be.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.174.195