I would like to add something to my $content variable to add on my node template file, but where is this variable composed? I assume it's in the template.php file but what specific function composes the content of the $content variable?


2 Answers 2


If you are referring to the $content variable that is available in node.tpl.php, its value is set in template_preprocess_node() from the following code:

  // Helpful $content variable for templates.
  $variables += array('content' => array());
  foreach (element_children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];

$variables['content'] is then passed to field_attach_preprocess().

  // Make the field variables available with the appropriate language.
  field_attach_preprocess('node', $node, $variables['content'], $variables);

To alter the content of the $content variable, you can implement hook_preprocess_node() (in a theme, or a module), or hook_field_attach_preprocess_alter().

Remember that what in a template file is $content, in other places is $variables['content'].

  • Great! Thanks for your help. The hook you gave me did the trick!
    – Michiel
    May 4, 2012 at 14:06

In template.php write this function

function hook_preprocess_page(&$variables) { // $variables will contain that content variable }

of you can use

function hook_preprocess_node(&$variables) { // $variables will contain that content variable }

Your Answer

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

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