2 invaluable Drupal development tips: list all available variables and backtrace a page

The Drupal Devel module includes some invaluable functions that make working with Drupal much much easier. A short list of these functions can be found at this post. One of these is the dpm() command (which I would guess stands for “Drupal Print Message,” or at least that’s how I remember it). Given an array or object, this function will output a div structure at the top of your page that you can use to visually walk through the contents of either of these sets of data. Combine this with existing PHP debugging and instrospection commands and you have a very useful and powerful development tool at your disposal. For example, place this at the top of your Drupal theme’s page.tpl.php template:

<?php
	 dpm( get_defined_vars() );
?>

Now when you navigate to your page you will have a clickable bar at the top that will list all variables available to the page when it loads, which you can access via the code in your theme page template if you want.

dpm_get_defined_vars

Or maybe you would like to know what path your page took through Drupal’s architecture to its final incarnation, use dpm() with PHP’s debug_backtrace() function. This will output an array of each function your page contents went through before they were output to the browser, plus it shows the location of these functions. Try this in place of the code snippet above:

<?php
	 dpm( debug_backtrace() );
?>

dpm_debug_backtrace

8 Responses to “2 invaluable Drupal development tips: list all available variables and backtrace a page”

  1. neil Says:

    hey….just a friendly tip. next time, put in 5 minutes of research before writing articles like this. the function you have called, dpm(), is not built-in to drupal, it comes with a module called devel. and if you have that module installed, you don’t really have to do all this manually, you can just check out the dev_load and dev_render tabs on each page….

    i put in the code without having drupal installed, and was wondering why the function was termed as undefined…

  2. Ans Says:

    Hi Neil Satra,

    Thank you for pointing out the Dev Load and Dev Render tabs for those that may not have been aware that they existed. However, I would give you the same advice. If you took 5 minutes to read my article you would have discovered that I say dpm() is part of the Devel module in the first paragraph. Also, using the Devel tabs as you suggested only gives access to the properties of the $node object, whereas dpm( get_defined_vars() ); returns all variables available to a particular Drupal page. Additionally, Dev Load and Dev Render do not provide page backtrace information.

    Perhaps you could contribute to the community knowledge base and write a post on Dev Load and Dev Render and post a link to it in the comments here?

  3. Marty Landman Says:

    I found this article informative and handy. Thanks.

  4. Rob Haag Says:

    I too found this “informative and handy”. I’m new to drupal and web development and little tips like this help a lot. Thanks.

  5. pablo Says:

    great article one question though is there anyway you can call the arrays that are generated from the dev renderer tab

  6. Ans Says:

    Hi Pablo,

    I’d have to look into it… but for the sake of giving you a quick reply, if you click on the devel tab I would think the arrays would become available as variables for the page, in which case you could view them using dpm( get_defined_vars() ); as above.

  7. Jared Says:

    Excellent article – very useful!!

    Thanks,
    Jared

  8. Marcelo Says:

    Very useful article. Thanks.

Leave a Reply