So I am getting the following notices and warnings:

* Notice: Undefined variable: node in include() (line 139 of page.tpl.php).
    * Notice: Trying to get property of non-object in include() (line 139 of page.tpl.php).
    * Notice: Undefined variable: node in include() (line 154 of page.tpl.php).
    * Notice: Trying to get property of non-object in include() (line 154 of page.tpl.php).

In all those files I am testing for a node type in order to position elements on the page and hide elements when appropriate. Here is an example of the code where I test for node type:
<?php if (!$node->type == 'project'): ?><?php print ('class="grid_8"');?><?php endif; ?>

Us this not the correct way to test for node types in page.tpl.php?

Comments

rgracia’s picture

No one...really?

robcarr’s picture

If it's any consolation, I'm mystified by the same error

tomogden’s picture

The $node object is only present when there's an automatically loaded node in the page. If it's not present, then you have to find some other way to initialize your variables and perhaps test to see if the $node object is present. Be sure to use the isset() function.

For example:

	# Initialize Variables
	$isProdType = false;
	$typetag = "";

	# Test if $node is present
	if ( isset($node) ) { # In case there is no automatically loaded node. 
		# Perform your Operations
		if(substr($node->type,0,5)=='page_') { 
			$typetag=substr($node->type,5); 
			$isProdType=true; 
		}else{ 
			$typetag=''; 
			$isProdType=false; 
		} 
	}

--
Tom/* Ogden

JohnDoranNY’s picture

I get the same error over and over and over... in Drupal 7... its a bug.

I can do a "print node->type" and it shows the node type exactly as it should on each different page of different node types.. so the node object exists.. I can do a print_r($node) and see all the stuff in the node.. and yet.. I still get the error message saying node does not exist even though drupal is printing all the node information to the screen. The node object does exist, and you can access/print the information but it throws errors along with the printed information.

oeroek’s picture

I tried to include $node->nid on page.tpl.php in order to make the page title clickable. Although $node->nid actually returned the correct nid and the system was functional I got this error message.
I am not sure what is best practice to return the current path of a page and to return the alias of a node.

sunchaser’s picture

same behavior here.

print $node->type

in page.tpl.php prints out the node type perfectly, but

if ($node->type == "[nodetype]") {
...
}

generates the Warnings (as above) ...

Jenya’s picture

Just like tomogden mentioned, you should use the isset() function before you check the type. A bit weird since D6 never needed to do this, but it's not that much more work. Something like this:

 if (isset($node)) {
    if ($node->type != 'blog') {
      print $title;
    }
  }
  else {
    print $title;
  }
Jennifer_M’s picture

Thanks for this thread, people - it was very helpful to me.

As far as I can tell, it's not a bug. It's a PHP error message being picky about the quality of the code.

See http://drupal.org/node/1045840#comment-4833960 for more related links/explanation/context.

(I suspect the reason D6 didn't always require the isset is not anything to do with D6 itself, but to do with the PHP version it's running on, and/or the level of error reporting set by default. I.e. it was technically bad practice in D6 too not to check for node's existence before using it, but the PHP let it go by :-) Just guessing here, I don't know for sure.)

Jenya’s picture

Exactly, this just follows programming standards more closely. PHP just reminds us the things all of us should have been doing since the start anyways.

andrea.cavattoni’s picture

You should use print $is_nodetype....