Instructions on top of the file mention about $node:

* - $node: The node object, if there is an automatically-loaded node
* associated with the page, and the node ID is the second argument
* in the page's path (e.g. node/12345 and node/12345/revisions, but not
* comment/reply/12345).

However, simple check
if ($node) {

produces an error:

Notice: Undefined variable: node in include() (line 175 of /home/mysite/public_html/sites/default/themes/mysite/templates/page.tpl.php).

Comments

yngens’s picture

If anyone is interested I had to replace check to

if (arg(0)=='node')

and it worked as desired. However, imo the above information in page.tpl.php should be modified not to confuse users.

johnalbin’s picture

Title: Variable $node is not available in page.tpl.php » Variable $node is not always available in page.tpl.php
Project: Zen » Drupal core
Version: 7.x-3.x-dev » 7.x-dev
Component: PHP Code » documentation
Category: bug » feature

That code comment comes straight from core's page.tpl docs. Moving to Drupal core.

jhodgdon’s picture

Status: Active » Closed (works as designed)

I am not sure what the documentation problem is here. The doc says that $node only exists in some cases, so if you do

if($node)

and $node doesn't exist, you are guaranteed to get an error on some pages.

So I'm closing this. If someone wants to explain what the doc problem is and reopen (i.e. what about the doc is unclear and could/should be clarified), please feel free.

samhassell’s picture

Status: Closed (works as designed) » Active

The docs are unclear as to when the node object is available.

I have met the requirements as specified in the docs (ie. on 'node/6') and $node is still not available.

I think this needs some clarification.

jhodgdon’s picture

Category: feature » support
Status: Active » Fixed

If you are on the page node/6 and node/6 exists, then $node should be available (as the documentation states). It sounds like you have something else wrong and need support.

Although you can create issues in Drupal Core and mark the category as "support request", we don't really handle support requests in the Drupal Core issue queue as a regular practice (that option is mostly there for filing support issues for contributed modules and themes).

There are several support options listed if you click on "Community and Support" at the top of Drupal.org, which will take you to:
http://drupal.org/community

There you can find out about the Drupal IRC channels, and the Forums, which are our two main support mechanisms in the Drupal community.

Good luck with your issue!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Jennifer_M’s picture

For the benefit of anyone else coming by to read this thread:

If you are on the page node/6 and node/6 exists, then $node should be available

That's true, but from what I've seen in researching a similar problem, it seems that even if $node is available on that particular page load, PHP may still throw the error based on its analysis of your code. To make PHP happy, you need an if isset($node), or equivalent, before you call $node itself. (I'm not familiar with yngens' bit of code in #1, but I suspect that it functions similar to an isset question.)

See
http://drupal.org/node/303492
http://drupal.org/node/1051824#comment-4561536
http://drupal.org/node/1051824#comment-4705798
http://drupal.org/node/37767#comment-4593838 & following bits of thread

Hope this helps someone.

(Not reopening the issue, as I don't think this information belongs in page.tpl.php documentation. The page.tpl.php documentation does what it needs to do, which is inform people that sometimes page.tpl.php doesn't have a $node. What I'm discussing here is really about how PHP deals with scope of variables - it's not specific to one template.)