By rgracia on
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
No one...really?
No one...really?
Sorry...
If it's any consolation, I'm mystified by the same error
Nodes are NOT Automatic
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:
--
Tom/* Ogden
Same stupid error
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.
error on $node->nid
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.
Same ...
same behavior here.
in page.tpl.php prints out the node type perfectly, but
generates the Warnings (as above) ...
Use isset()
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:
Thanks, & some relevant info which may help others
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
issetis 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.)Well Written Code
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.
You should use print
You should use print $is_nodetype....