Reinstall, inspect modules, and generate data

The Drush extension of the Devel module provides a set of tools to speed up the development process of Drupal projects. This module's commands are for:

  • Reinstalling enabled modules: disable, uninstall, and reinstall a module with just one command
  • Search and inspect implementations of hooks and functions within all modules
  • Generate users, nodes, taxonomies, and menus on the fly

Installing the module

Devel module is installed like any other module. We will take the chance to install devel_generate submodule too, as we will use it in the examples.

$ cd /home/juampy/projects/drupal
$ drush pm-download devel
Project devel (7.x-1.2) downloaded to /home/juampy/projects/drupal/sites/all/modules/contrib/devel. [success]
Project devel contains 3 modules: devel_generate, devel, devel_node_access.
$ drush pm-enable --yes devel devel_generate
The following extensions will be enabled: devel, devel_generate
Do you really want to continue? (y/n): y
devel_generate was enabled successfully. [ok]
devel was enabled successfully. [ok]
FirePHP has been exported via svn to /home/juampy/projects/drupal/sites/all/modules/contrib/devel/FirePHPCore. [success]

Right after Devel module was enabled, the FirePHP was downloaded and installed within the module path. This was achieved by implementing the hook drush_MODULE_post_COMMAND(). The following is its implementation extracted from the Drush command file within the module. It first checks if the Devel module was asked to be enabled and if so, calls another function to download FirePHP.

/**
* Implements drush_MODULE_post_COMMAND().
*/
function drush_devel_post_pm_enable() {
$extensions = func_get_args();
// Deal with comma delimited extension list.
if (strpos($extensions[0], ',') !== FALSE) {
$extensions = explode(',', $extensions[0]);
}
if (in_array('devel', $extensions) && !drush_get_option('skip')) {
drush_devel_download();
}
}

Reinstalling modules

Sometimes we need to reinstall a module, for example, when you change the schema of a new module under development. This process involves three commands (pm-disable, pm-uninstall, and pm-enable). The Devel module implements a command to accomplish this: devel-reinstall. In the following example, we will download and install the Twitter module and then reinstall it again using this command:

$ cd /home/juampy/projects/drupal
$ drush pm-download twitter
Project twitter (7.x-3.0-beta4) downloaded to [success] /home/juampy/projects/drupal/sites/all/modules/contrib/twitter.
Project twitter contains 4 modules: twitter_signin, twitter_post, twitter_actions, twitter.
$ drush pm-enable --yes twitter
The following extensions will be enabled: twitter
Do you really want to continue? (y/n): y
twitter was enabled successfully. [ok]
$ drush devel-reinstall --yes twitter
The following extensions will be disabled: twitter
Do you really want to continue? (y/n): y
twitter was disabled successfully. [ok]
The following modules will be uninstalled: twitter
Do you really want to continue? (y/n): y
twitter was successfully uninstalled. [ok]
The following extensions will be enabled: twitter
Do you really want to continue? (y/n): y
twitter was enabled successfully. [ok]

As we can see in the previous output, devel-reinstall calls the project management commands internally to disable, uninstall, and reinstall the module for us. Here is the command callback that creates an array with the three commands to execute and then calls drush_invoke() to run them:

/**
* A command callback. This is faster than 3 separate bootstraps.
*/
function drush_devel_reinstall() {
$projects = func_get_args();
$args = array_merge(array('pm-disable'), $projects);
call_user_func_array('drush_invoke', $args);
$args = array_merge(array('pm-uninstall'), $projects);
call_user_func_array('drush_invoke', $args);
$args = array_merge(array('pm-enable'), $projects);
call_user_func_array('drush_invoke', $args);

}

Inspecting source code

In Drupal sites, source code is spread among a long list of modules. This makes having precise tools, for finding hook implementations or functions, within a directory essential. Devel makes this process easier, by providing a command to search for implementations of a hook (fn-hook) or for any function or method (fn-view). We will now go through each of these two commands.

Searching for hook implementations

The fn-hook command has a simple syntax and interactive flow. By providing a hook name, a list of modules implementing it, is presented onscreen as a set of choices. Selecting one of these, prints the file name, the line number, and the source code. Here is an example where we look for implementations of hook_node_insert() and then choose the one in Comment module:

$ drush fn-hook node_insert
Enter the number of the hook implementation you wish to view.
[0] : Cancel
[1] : comment
[2] : menu
[3] : path
1
// file: /home/juampy/projects/drupal/modules/comment/comment.module, lines 1288-1303
/**
* Implements hook_node_insert().
*/
function comment_node_insert($node) {
// Allow bulk updates and inserts to temporarily disable the
// maintenance of the {node_comment_statistics} table.
if (variable_get('comment_maintain_node_statistics', TRUE)) {
db_insert('node_comment_statistics')
->fields(array(
'nid' => $node->nid,
'cid' => 0,
'last_comment_timestamp' => $node->changed,
'last_comment_name' => NULL,
'last_comment_uid' => $node->uid,
'comment_count' => 0,
))
->execute();
}
}

As we can see, after selecting one of the choices, we get the full path to the file that contains the hook, and the start and end lines within that file. After that is the hook source code, which saves us from opening the file with a text editor.

Tip

The choice list is formatted using drush_choice(), which receives the list of options, a text, and how each option is presented.

Viewing source code of functions and methods

If you know the name of the function or method that you are looking for, you can use fn-view to print its contents, and even open the file which contains it by passing its full path to a text editor.

In the following example, we print the source code of the function block_get_blocks_by_region(). Note that the full path, and start and end line numbers are printed at the top of the output:

$ drush fn-view block_get_blocks_by_region
// file: /home/juampy/projects/drupal/modules/block/block.module, lines 313-319
/**
* Get a renderable array of a region containing all enabled blocks.
*
* @param $region
* The requested region.
*/
function block_get_blocks_by_region($region) {
$build = array();
if ($list = block_list($region)) {
$build = _block_get_renderable_array($list);
}
return $build;
}

Now, we want to open the file (where the function is) with a text editor. In order to achieve this, we will use the option --pipe, which prints the filename instead of the source code. We will first test it and then wrap it with left ticks which will pipe the file path to our preferred editor (in this example, Vim):

$ drush fn-view --pipe block_get_blocks_by_region
/home/juampy/projects/drupal/modules/block/block.module
$ vim `drush fn-view --pipe block_get_blocks_by_region`

The previous command will launch our text editor and open the file. In the previous chapters, we have used the left ticks for piping the output of a command for another command (as we did with the sql-connect and sql-dump commands in Chapter 2,Executing Drush Commands).

Generating users and nodes

Devel can create users, nodes, taxonomies, and menus for us with a single command. This is a killer feature in the early stages of a project when we need some fake data to populate our site and to how it would look like when it gets real data.

Here are a few examples of how you can generate data of different kinds:

  1. Create 20 nodes of type page with a maximum of 15 comments per node and delete all previous content by using the option --kill:
    $ drush generate-content --kill --types=page 20 15
    Finished creating 20 nodes [status]
    Generated 20 nodes, 15 comments (or less) per node. [success]
    
    
  2. Create 2 menus with 40 links, 5 levels of depth and 10 links maximum at the first level. Delete previously generated menus before starting:
    $ drush generate-menus --kill 2 40 5 10
    Generated 2 menus, 40 links. [success]
    Deleted existing menus and links. [status]
    Created the following new menus: [status] tiphite
    pradre
    Created 40 new menu links. [status]
    
    
  3. Create a taxonomy vocabulary and add 50 terms to it:
    $ drush generate-vocabs 1
    Generated 1 vocabularies. [success]
    Created the following new vocabularies: sipuwake [status]
    $ drush generate-terms sipuwake 50
    Generated 50 terms. [success]
    Created the following new terms: [status]
    guuo
    shimobobi
    gulauetihuni
    ...
    
    
    • In the previous output we used two commands. The first one (generate-vocabs) created a vocabulary called sipuwake, which we used in the second command (generate-terms) to create 50 terms for it.
  4. Create 20 users with role IDs 4 and 5, and delete all users before starting:
$ drush generate-users 20 --roles=4,5 --kill
Generated 20 users. [success]
20 users deleted. [status]
20 users created. [status]

The commands used in the previous examples (generate-content, generate-menus, generate-vocabs, generate-terms, and generate-users) are more than enough to populate a site with testing data, saving us from clicking around the administration interface hundreds of times creating nodes, users, vocabularies, and terms. These commands try to populate as much data as possible, for example, if we create nodes from a content type that has images, it will create and upload them.

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

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