1

For Drupal 7, how do I access (or generate) a list of all the field variables available for building a template (for a given content type)?

Some context: I used to rely on the (now deprecated) Contemplate module to write my templates. Directly within its interface, it would print a list of all of the available variables, like this.

contemplate

It was quite handy. Now that I'm making disk-based templates and do not have this list generated by the Contemplate module, I'm confused as to how I might produce something similar for use as a reference.

3

1 Answer 1

1

Use Devel module and then with using get_defined_vars you can get a list of defined variables. then in you template file use the following snippet

<?php
 dsm(get_defined_vars()); 
// or even  dsm(get_defined_vars()); 
?>

and another solution is based on hook_preprocess. just dumps the variables there.

on your template.theme file

mymodule_preprocess(&$vars, $hook) {
dpm($vars);
}

**Update* if you want to see the $variables on node template try on your template.

copy/paste this snippet on your template.php then clear the cache function YOURTHEMENAME_preprocess_node(&$variables) { dpm($variables); // you can add or remove whatever you want to it here }

if your theme name is themetastic So

function themetastic_preprocess_node(&$variables) {
    dpm($variables);
    // you can add or remove whatever you want to it here
}

Ref: Read this to learn more about preprocessing

10
  • Can I write this snippet into a php block and put it on a node (of a specific content type)? I tried this, and had promising results, but not exactly what I needed. It printed out what looks like the site's global variables, but not the node's field variables. Is there a way to do this without modifying the template file yet?
    – Sam
    Commented Mar 22, 2019 at 13:20
  • You'd have to do that in a preprocess hook in PHP @Sam (dpm($variables);). Each template is executed in its own scope, with its own bunch of available variables, so executing get_defined_vars will only give you something useful if you execute it directly in the template you want to find the variables for
    – Clive
    Commented Mar 22, 2019 at 13:25
  • @Sam you can do it for an specified hook. as an example. mymodule_preprocess_block or mymodule_preprocess_node. do you how you can use those?
    – Yuseferi
    Commented Mar 22, 2019 at 13:55
  • @YusefMohamadi Thanks! No, I don't know how to use those (but I'm Googling it now!). Any more tips here would be awesome!
    – Sam
    Commented Mar 22, 2019 at 13:58
  • @Sam do you have custom module or theme?
    – Yuseferi
    Commented Mar 22, 2019 at 14:14

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.