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

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

}
mrroy69Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James RodgersWeb Applications DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrroy69Author Commented:
Thank you sir!
James RodgersWeb Applications DeveloperCommented:
Glad I could help.

thanks for the points
Aaron FeledyDrupal Developer and ConsultantCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Drupal

From novice to tech pro — start learning today.