Download & Extend

$node Variable not Accessible in Page Template for Nodes That Match Rule

Project:Node breadcrumb
Version:6.x-1.0-beta6
Component:Code
Category:bug report
Priority:major
Assigned:Unassigned
Status:active

Issue Summary

I created a simple rule that assigns a certain menu item for a content type. As soon as that is done I don't have a $node object anymore when I access it in my page.tpl.php. When I delete the rule or deactivate node_breadcrumbs everything works as intended. Am I missing something or is this a bug?

Comments

#1

Drupal does not set $node variable in page.tpl.php. Do you set it in template.php? Show your code where you init it.

#2

It doesn't? Ok, then I have a fundamental understanding problem. I never initialize $node, I just use it. But I also checked and I cannot access $node directly in the page.tpl.php.

I should clarify. I have a custom theme, in the page.tpl.php I call a function that renders some links for the current node based on the users permission

<?php print dart_render_operations($node) ?>

In my template.php file the actual function is

function dart_render_operations ($node) {
    $menu_item = menu_get_item();
    $active_path = $menu_item["href"];

    $output = "";
    if (node_access("create", $node->type)) {
...
    }
    if (node_access("update", $node)) {
...
    }
    if (node_access("delete", $node)) {
...
    }

    return $output;
}

This way the $node variable is set correctly and I can access it. However, I never initialized it knowingly. I've seen this usage in some default theme and just assumed that it is there (and it worked curiously).
But as you said I cannot access $node from the page.tpl.php. So I'm assuming it is injected from Drupal's template mechanism somewhere when the template is parsed?

Anyway, using this construction everything works when I don't have a rule defined for node_breadcrumb. As soon as I define a rule for a content type the above routines don't work anymore.

#3

http://drupal.org/node/572718 - similar issue

Add this code to dart_render_operations as first line:

if (!$node && arg(0) == 'node' && is_numeric($nid = arg(1))) $node = node_load($nid);

PS: Here vars for page.tpl.php http://api.drupal.org/api/drupal/modules--system--page.tpl.php and there are no $node var.

#4

According to this page http://drupal.org/node/11812 , a $node variable was available to page.tpl.php in D5

Amazingly, I have not found it in D6.

#5

But it also says "If you are in page.tpl.php displaying a node in full page view then $node is available to your template."

Are you in a full page view?

#6

There is an issue here. With a node that hasn't been processed by node_breadcrumb, I can do this in my theme's template.php:

<?php
function example_preprocess_page(&$vars) {
 
print_r($vars['node']);
}
?>

This will output the data associated with the current node (assuming there is one). However, for nodes that have been processed by node_breadcrumb, the above prints nothing.

There's a pretty easy workaround:

<?php
function example_preprocess_page(&$vars) {
  if ((
arg(0) == 'node') && (is_numeric(arg(1)))) {
    if (!
$vars['node']) $vars['node'] = node_load(arg(1));
  }
 
print_r($vars['node']);
}
?>

This checks to see if $vars['node'] exists, and if it doesn't, uses node_load and arg(1) to load the node where it should be. However, ideally node_breadcrumb probably shouldn't be subverting $vars['node'] in the first place.

Having said that, a fantastic module that has saved me a ton of hassles.

#7

I agree, in that case, it looks pretty obvious that whatever function is building the breadcrumb is doing an unset (or something similar) to $vars['node'].

Not sure why it would do that? Not being something I have used a lot, I would say that it's going to be something silly like unsetting $vars generally, and then setting $vars['breadcrumb'] = X.

Just a general thought, although I could be wrong, I usually am.

#8

Thought as much, just went through the code and couldn't see anything immediately obvious like I hoped there might be. I agree that your workaround is good in-so-far as it works, but surely we shouldn't have to have a workaround for wsomething like this?

I mean, Drupal is based on nodes, and breadcrumbs are pretty basic functional elements in the grand scheme of things, surely you should be able to use the two together without having to use a workaround?

I'm going to do a bit more digging on this when I get time. If I can suss it out then it might be useful to someone.

#9

Priority:normal» major

Actually, it is documented as being available at the template level as such, pulled from D6.19, /modules/system/page.tpl.php:

* - $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).

So apparently what we (as I'm having the same problem all of a sudden) seem to be running into is that somehow the system is convinced the node path isn't valid, so it's not loading the node into the global variable space for the template.

Since this breaks existing functionality of templates following Drupal-documented variable availability, I'm raising it to "major" as it needs to be addressed. I'll poke around as well and see if I can identify where it's happening...

#10

This looks suspiciously like what was reported earlier in #620038: node_breadcrumb causes Disqus comments to not show up where the menu_get_item() call was failing in another module. It's beginning to look like something in the way the function _node_breadcrumb_set_location() is manipulating the menu route table is breaking the ability to reference the currently loaded node in certain parts of the theming layer. I actually put the following code at the top of my page.tpl.php file, which in theory should have loaded the node oject if empty:

<?php
 
if (empty($node))
   
$node = menu_get_object();
?>

No valid object was returned. According to a.d.o (http://api.drupal.org/api/function/menu_get_object) this should have worked from anywhere in the system. In fact, that same function is what is called by template_preprocess_page() in D6 core (follow the "functions call" links in the API page) to populate the $node variable that is documented in the above comment to be available in page.tpl.php.

Definitely needs to be looked into a bit more...

#11

I'm experiencing a bug here too — if I've enabled this module for a given content type, $node no longer exists — as if it's 'unset' — in the page.tpl.php file. It's way odd.

#12

You might have saved my tail. I spent 6 hours without thinking of this. I can't get access to the damn #node variables and now I can. Thank you.

#13

I know this post is pretty old...but I was just pulling out $node content and found that it's available in the node.tpl.php file, not the page.tpl.php file.

#14

any updates on this topic? this actually screws up template logic...

#15

I'm giving up on this module for now because support is slipping and I can't get by with this type of side effect.

I've switched to http://drupal.org/project/menutrails to get similar functionality. Hope this manages to help someone.

nobody click here