1

Calling debug($node) in page.tpl.php seems to show that the node object is not fully populated at this stage.

I would like to access node_url in my page template so that I can add certain CSS classes for some nodes without having to use the nid. Is there maybe a way to access a more fully populated node object in template_preprocess_page()?

2 Answers 2

5

You could use the drupal_get_path_alias() function along with the node's nid to get the URL alias of the node without having to load the object:

$url = drupal_get_path_alias("node/$node->nid");

If you want the fully loaded node object though use node_load():

$node = node_load($node->nid);

Memory fails me as to whether node_url is added to the node object in a hook_node_load() implementation or as part of a theme preprocess so I'm not 100% sure it's implicitly available after you call node_load()...if it's not just use the first example which will work.

UPDATE

On inspecting the node module the node_url member comes from template_preprocess_node so while node_load() will give you the fully loaded node, I don't think it's going to be useful in this case.

In fact looking at node.tpl.php and template_preprocess_node, node_url isn't added to the node object at any point so you wouldn't normally access it with $node->node_url but instead $node_url. That variable is available by default in node.tpl.php but not page.tpl.php so you'll have to get it another way (i.e. first example above).

0
1

$url = drupal_get_path_alias("node/$node->nid");

The above will return the URL path, but NOT the ?q= if you are not using clean URLS. Just thought I'd mention that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.