4

In Drupal 7, you can use the following in template.php to test for the presence of a node type:

function MYTHEME_preprocess_node(&$variables) {
if ($variables['type'] == 'NODETYPE') { 
***CUSTOM CODE****
}

In Drupal 7, you can use the following in template to create a variable which can be printed out in your page.tpl.php file:

function MYTHEME_preprocess_page(&$variables) {
      $variables['greeting'] = 'Custom Greeting';   
} 

But how do you combine the two, to create a variable inside of template.php (that can be printed in page.tpl.php) which changes according to the node type?

I would have thought the following would work:

    function MYTHEME_preprocess_page(&$variables) {
              if ($variables['type'] == 'NODETYPE') { 
  $variables['greeting'] = 'Custom Greeting';
    }
    } 

But it doesn't work. Why is this? (Using the same code on MYTHEME_preprocess_node also does not work).

Thanks for any help!

| improve this question | | | | |
6

Each template has a different set of variables available. The variables available in a given theme is generally documented in the comment section at the top of the file. You can consult this documentation on http://api.drupal.org.

The node type is not directly available as a variable in the page.tpl.php. However, you have the $node object which has a type property:

function MYTHEME_preprocess_page(&$variables) {
  if (!empty($variables['node']) && $variables['node']->type == 'NODETYPE') {
    $variables['greeting'] = 'Custom Greeting';
  }
} 
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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