Devel

Featureset

The devel module is an invaluable tool for constructing and coding websites. It has many features, such as dprint_r(), dsm(), a clear_cache button, and a generate_random_nodes feature. Devel is usually used for low-level development of a site, while the also-loved Util.module handles site-wide administrative features that are cool, and just don't exist in core for one reason or another.

What does dprint_r() do?

The dprint_r function is a really cool tool for peeking inside of a Drupal array to see what's there. It "pretty-prints" the array into a human readable paragraph, rather than just spewing all the variables out onto the page one after the other in an endless and mostly incomprehensible strand like PHP's print_r() function would do. There are several ways to implement it, and all of them require the devel module in order to work.

// Example of an array viewed with Devel's dprint_r()

Array
(
    [form] => Array
        (
            [568717fb5880cf349c074f20f92ef326] => Array
                (
                    [timestamp] => 1189457316
                    [args] => Array
                        (
                            [0] => system_modules
                        )
                )

// Example of an array viewed with PHP's print_r()

Array ( [form] => Array ( [568717fb5880cf349c074f20f92ef326] => Array ( [timestamp] => 1189457316 [args] => Array ( [0] => system_modules ) )

Which one is easier to read? Now you know why you want to use this snippet, so let's get to it!

Example 1 - printing an array from within a custom module

Here's the code snippet:

if (module_exists(devel)) {
  dprint_r($_SESSION);
}

It's important to know that sometimes, depending on where your particular module falls in the core's own build process, you might be able to generate a fatal error by trying to call dprint_r() from inside your own module. Even if devel exists, it might not be fully rendered as a set pf php functions yet. If this happens to you, simply move this code snippet inside one of your module's functions rather than leaving it naked at the top of your module, and the error will stop.

Other array variables you can use:
The $_SESSION array is just one example. You could use dprint_r($GLOBALS) to see all global variables in use at that moment, or dprint_r($_POST) to show the last form submission's $post array, or even dprint_r($_COOKIE) if you needed to find out what the user's cookies were storing for you at that moment in time.

Example #2 - Printing an array directly into a theme's output

Place this into your node.tpl.php to show the contents of the first node. Replace the "1" with any number of your choice, as long as it's a node that exists in your site.

<?php
if (module_exists('devel')) {
 
$node = node_load(1);
  
// Show what's in node 1
 
dprint_r($node);
}
?>

Controlling placement on the page:
You can generate the printed array into a block, into a footer, or into a $messages box, like the "login successful" message that Drupal prints out automatically when a user logs in.
To print in a block, see http://api.drupal.org/api/function/hook_block/5, footer, see http://api.drupal.org/api/function/hook_footer/5, and $messages, see http://api.drupal.org/api/function/drupal_set_message/5

How can dsm() help me?

The dsm() function prints any variable directly into the drupal $messages box on your page. What's a $messages box? It's that specially-colored area in your theme that Drupal uses to announce cool happenings such as "Created new taxonomy term for node/4312". Arrays printed with this function during module development are far less likely to wreck your beautiful page layout, and it also allows you to more easily see the content of your nodes during development, when contrasted with the dprint_r() function above.

 
 

Drupal is a registered trademark of Dries Buytaert.