0

I get the following notices on every page:

Notice: Undefined index: node in myZen_preprocess_page() (line 242 of /Applications/XAMPP/xamppfiles/htdocs/drupalH1/sites/all/themes/myZen/template.php).
Notice: Trying to get property of non-object in myZen_preprocess_page() (line 242 of /Applications/XAMPP/xamppfiles/htdocs/drupalH1/sites/all/themes/myZen/template.php).
Notice: Undefined index: node in myZen_preprocess_page() (line 246 of /Applications/XAMPP/xamppfiles/htdocs/drupalH1/sites/all/themes/myZen/template.php).
Notice: Trying to get property of non-object in myZen_preprocess_page() (line 246 of /Applications/XAMPP/xamppfiles/htdocs/drupalH1/sites/all/themes/myZen/template.php).

I put the function I need to use the files page--content-type, and is as follows:

function myZen_preprocess_page(&$vars) {
   if ($vars['node']->type == 'mappe'){
     $vars['theme_hook_suggestions'][] = 'page__mappe';

    }
  else if ($vars['node']->type == 'fatti_e_risorse'){
    $vars['theme_hook_suggestions'][] = 'page__fatti_e_risorse';

  }

  }

Can I disable notices or fix this? thank you!

1 Answer 1

3

That notification is displayed because the node key of the $vars array is not always defined in hook_preprocess_page() function.

Use the following code, which first checks if the node key exists and then continues. It also uses the switch statement, which is easier to read if you only need to check the node type and use one condition:

function myZen_preprocess_page(&$vars) {
  if (isset($vars['node'])) {
    switch ($vars['node']->type) {
      case 'mappe':
        $vars['theme_hook_suggestions'][] = 'page__mappe';
        break;

      case 'fatti_e_risorse':
        $vars['theme_hook_suggestions'][] = 'page__fatti_e_risorse';
        break;
    }
  }
}
1
  • Worked for another function I was running. Thanks! Commented Mar 20, 2014 at 6:45

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.