Introduction

This is a very simple snippet I use to display all the Drupal variables available to a page when I'm testing/debugging a site.

Step 1 of 1

Using a text editor like Pspad or notepad, open up your node.tpl.php file and paste in the following snippet at the bottom of the file.

<?php
  print '<pre>';
  var_dump(get_defined_vars()); 
  print '</pre>';
?>

An alternative: using dpm()

If you have installed and enabled devel module, you can view the variables by adding this code to your .tpl.php file:

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

You will then see a yellow clickable output, which allows you to see which variables are available.

Image of dpm() output

AttachmentSize
dpm-variables.png51.47 KB

Comments

akhilsoni’s picture

Thanks

oliversiegel’s picture

Any updated instructions for this in Drupal 8 or 9?

I'd like to see what variables I have available when using my mytheme.theme file. Calling dpm() doesn't seem to do anything

Or even I might like to just take a look into the $variables that I have available within a hook in that theme file.

print_r() not only runs out of memory, but it's also a bit clunky... I wanna see what's inside the black box tho ! lol

ThirstySix’s picture

Use devel module and then Try with dpm() or dvr() functions to reduce the memory.

ressa’s picture

The easiest way may be to install Devel module and use it like this?

function hook_preprocess_html(&$variables) {
  debug($variables);
}
maxilein’s picture

where would you put this?
Especially when your theme has only twig files?

ressa’s picture

Like @ThirstySix recommends, dpm() is probably better. Add it to the THEMENAME.theme file, right after the function hook_preprocess_html() declaration. For example in the Olivero theme, add it around line 21 in /core/themes/olivero/olivero.theme, like this:

function olivero_preprocess_html(&$variables) {
  dpm($variables);

Use dvr($variables); to get only the variables.

See https://www.drupal.org/docs/contributed-modules/devel/dpm-and-other-deve... for more.