5

I am developing a Drupal theme from scratch, and I have run into some trouble with template suggestions. Using the solutions discussed on Drupal support, I created a template.php file and included this code:

function stronger_preprocess_page(&$vars) {

  if (isset($vars['node'])) {

    $vars['theme_hook_suggestion'] = 'page__'.$vars['node']->type; //
  }
}

This has allowed me to successfully use page--nodetype.tpl.php template files, but I am also randomly seeing errors such as:

Notice: Undefined variable: node in include() (line 24 of /Applications/MAMP/htdocs/strong-local-final/sites/all/themes/stronger/page.tpl.php).

I have read elsewhere that the node variable needs to be re-declared, but I am unsure of how or where to do this. Any help or references would be greatly appreciated!

4

hook_preprocess_page() is run for any page, even non-node pages. If you're getting that error it means your trying to access $node in the page.tpl.php file when you're on a non-node page. As no node is available on such a page, the variable won't have been set.

Because of the delay that can happen when messages are set in template files, you might only see this message when you've left a non-node page, and navigated to a node page. So it will look like it's that particular page complaining, when in fact it was the previous one.

You should be able to fix the problem by checking the $node variable is available in your template file as well as in the preprocess function. e.g.

<?php if (isset($node)) : print $node->something; <?php endif; ?>
  • Thanks @Clive! Bear with me as I work to understand this. Do I need to be checking for the $node variable in the template.php file or the page--nodetype.tpl.php file? – Susan Jul 10 '12 at 20:18
  • From the error it looks like the page.tpl.php file is the culprit (/Applications/MAMP/htdocs/strong-local-final/sites/all/themes/stronger/page.tpl.php), but yes it's the template file you need to add the check in, not the preprocess function – Clive Jul 10 '12 at 20:20

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.