In Drupal 7 I'm trying to access the body field of a page from template.php. I want to find a keyword in the text and then send subsequent text to a separate variable so that I can format it in page.tpl.php

I can access the node using $vars['node'] but can't find a way of accessing the content part of the node.

I can access the content in page.tpl.php using: $node->body[$node->language][0]['value'] but cant use the same or similar syntax in template.php

1 Answer 1


In page.tpl.php the content of the page is available in $page['content']. It seems that your are mixing two different concepts here: pages, any page on the site (including administration page, taxonomy terms page, and other not directly editable pages) and nodes, editable content. This can be rather confusing as a default Drupal installation provides a page content type. So you end up with page nodes, which are nodes of a the page content type.

The layout of the content of a page displaying a node, is handled in the node.tpl.php template. In this template, the body of the displayed node is available in $content['body'].

If you really need to access a field (handled using the Field API) of the node object ($node) from the page preprocess function in your template.php, you can use:

function THEME_preprocess_page(&$variables) {
    $node =& $variables['node'];
    $value = field_get_items('node', $node, 'FIELDNAME);

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.