Drupal Notice: Undefined variable: node on page.tpl.php

mrroy69
mrroy69 used Ask the Experts™
on
I keep getting notice errors like "Notice: Undefined variable: node" with the following code on page .tpl when I'm in the admin area eventhough I'm excluding admin pages


if (!path_is_admin(current_path())) { // Only for non-admin pages

// Define node id
if($node->nid){
$mynodeid = $node->nid;
} else {
$mynodeid = '';      
}
// Define node id

// Define page type
if($node->type){
$mynodetype = $node->type;
} else {
$mynodetype = '';      
}
// Define page type

}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Web Applications Developer
Commented:
https://api.drupal.org/api/drupal/includes%21path.inc/function/path_is_admin/7.x

from the page
By default, paths are considered to be non-administrative. If a path does not match any of the patterns in path_get_admin_paths(), or if it matches both administrative and non-administrative patterns, it is considered non-administrative.

so if the path matches an admin and a non-admin the result is non-admin

check if the node is set, by your code there may not e a node object available, but the condition still tries to check the node, so node is not defiend
// set default values
$mynodeid = '';   
$mynodetype = '';     
 
// set to node values if avaialble
if(isset($node)) {
	$mynodeid = $node->nid;
	$mynodetype = $node->type;
}

Open in new window


if a node is set it has an id and type


or updating your code

if (!path_is_admin(current_path())) { // Only for non-admin pages

// Define node id
if(isset($node) && $node->nid){
	$mynodeid = $node->nid;
} else {
	$mynodeid = '';      
}
// Define node id

// Define page type
if(isset($node) && $node->type){
	$mynodetype = $node->type;
} else {
	$mynodetype = '';      
}
// Define page type

}

Open in new window

Author

Commented:
Thank you sir!
James RodgersWeb Applications Developer

Commented:
Glad I could help.

thanks for the points
Aaron FeledyDrupal Developer and Consultant

Commented:
Depending on your needs and preference, another option is to copy your page.tpl.php into a new file called page--node.tpl.php. Any code in this file will only be processed when the page is displaying a node and will eliminate the need to test for it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial