4

In the node template, I used to put the following code to find the variables:

<?php print_r($node) ;?>

What if this is a user account page (e.g. /user/1), what "$object" should I print to see the variables. I need to pre-fill the webform's fields with the profiler2's fields.

2 Answers 2

6

Try get_defined_vars

If you install the Devel Module there is a page for viewing and editing variables.

Also you can try

<?php
 dsm (array_keys(get_defined_vars())); 
?>

See also:

get_defined_vars()

get_defined_constants()

get_defined_functions()

3

Variables for template files are set in their preprocessor functions, for example the node template has template_preproess_node(&$variables).

Any module or theme can hook into these preprocessor functions to set/modify/remove variables, which will then be available in the template file.

For example if in the preprocessor you set:

$variables['my_new_variable'] = 'This is some text in a new variable';

You can then use $my_new_variable in the template file.

Now after all the preprocess functions are called, then there are process functions, which for the node template would be template_process_node(&$variables).

So most of the time variables are added in preprocessors, so to see all the variables you should check in the processor.

So in your theme's template.php file add:

/**
 * Variable processor for the node template.
 */
function THEMENAME_process_node(&$variables) {
  // Print the variables to the screen.
  print_r($variables);
  // A better alternative is to install the devel module and then do this.
  dpm($variables);
}

In the above example change THEMENAME to the name of your theme. As noted in the above example, you get a much better result if you install the devel module and use the dpm() function, instead of print_r(). Note that when you use dpm() in templates & preprocessors it takes 2 page refreshed to see you message instead of 1.

You will also usually get a satisfactory result by doing this in the equivalent preprocess function instead of the process function.

Now there is still a (negligible) chance that after your process function is run, that another implementation of the process function. If you really need to (you most likely don't), update the weight of your theme in the settings table of the database and make it a very high number, so it runs last. Note: This is more advanced and if you are unsure of what I'm talking about you are probably best not to mess with this. It likely isn't necessary anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.