Drupal developers' toolkit: handy tools and functions to make your life easier

{% raw %}
Drupal is big and complex, and no matter how good you are, you will need to debug. Before you get started coding for a Drupal site, you should download and enable the very useful devel module on your dev site. Go ahead, I'll wait.

This primer covers all the tools that I'll use in later tutorials on this blog. In the real world I end up using 5-6 of these guys regularly. Learn them. They help.


Printing a message is the basic tool of any debugging, and you need to know this core function. This lets you print a message to the page, Drupal-style. Usage: drupal_set_message($message, $severity).

drupal_set_message(t('Hello %name!', array('%name' => 'John Doe')), 'warning'); 

Note that I use the t() function to process text in drupal_set_message. This is partly because the function itself doesn't do any text sanitizing, so it's good practice to insert a layer there. It's also because you can't  insert variables directly into the message, and t() is a convenient way of doing that.

Note that drupal_set_message() does not do well with large messages - it is not recommended to print big arrays directly to the message area. For that, we'll use another function:


This sets a variable of your choice to Drupal's messaging system, in a nice compact output handled by krumo. 
Most of Drupal is understandable and learnable on the fly with just this simple tool. Usage: dpm($variable, $name = NULL).

dpm($node, 'This is the node variable');


Just like dpm, but it prints to the page header rather than the Drupal message area. Helpful when your theme doesn't print messages. You can also have it return a string instead of printing it automatically, which comes in handy in some fairly bizarre circumstances. Usage: kpr($variable, $return = FALSE, $name = NULL). 

kpr($node, FALSE, 'This is the node variable');


OK, you don't like krumo. Maybe you're a masochist, or maybe you have some esoteric requirements to deal with. Either way, you want dvm(). It prints variables in a more traditional format into the message area. I have used this to get variables into pastebin, very helpful when asking for help on Drupal IRC channels. :) Usage: dvm($variable, $name = NULL).

dvm($node, 'This is the node variable');


If your theme doesn't display messages, dpr() prints variables to the page header without krumo. Same usage case as above. Just like kpr(), you can return a string with this function. Usage: dpr($variable, $return = FALSE, $name = NULL).

dpr($node, FALSE, 'This is the node variable');


This guy shows you the arguments passed into the current function, using krumo for readability. This is my go-to function when using Drupal hooks. The API documentation is great, but there's nothing like simply seeing the variables you have to work with. Usage: dargs()

function swearing_custom_form_alter(&$form, &$form_state, $form_id) {


For those awful times when a hook doesn't produce direct output, there's always dd(). This guy prints a given variable to a file called "drupal_debug.txt" in your site's temporary files directory. This comes in very handy. Usage: dd($variable, $name = NULL)

dd($node, 'This is the node variable');


This prints a backtrace for the current function in krumo, in the head of your current page. Devel module comes with the ability to enable this automatically for fatal errors, but occasionally you just want to see how things work. Usage: ddebug_backtrace().


This tool is handy for testing queries. Devel also has the ability to display a query log at the bottom of every page, showing every query with a time-to-execute. That can be pretty overwhelming. db_queryd() lets you put in a single query and see if your database spits out any errors. Usage: db_queryd($query, $arguments = array())

{% endraw %}
comments powered by Disqus