OK, showing my ignorance...

I presumed that php code in the node body would have access to the node object and other objects/variables that are available to node.tpl.php e.g. I have tried the following without success:-

Other content items...
<?php 
   print $terms;
   print $taxonomy;
   print $node->nid;
?>
More content....

The node's nid is the most important so that I can use it to use function calls.

Comments

efolia’s picture

The $node object is already processed once you are at a page level. In order for your code to process $node, you first need to get the $node object like this:

 if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
// some code to process the $node object 
  }

This will retrieve the node id from the path, even if it is aliased. The node_load function loads the node object in $node.

efolia

nevets’s picture

$node should be available by default in node.tpl.php so $node->nid should work, $terms will should work if the node as taxonomy terms associated with it. Try adding the following at the end of your theme's node.tpl.php file

<pre>
<?php
print print_r($node, TRUE);
?>
</pre>

It should should the complete node.

dman’s picture

$node->nid should certainly work, although I've seen it go missing in some themes.
Here's the docs

See also

<pre>
<?php
print print_r($variables, TRUE);
?>
</pre>

to get access to absolutely everything available.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

John Bryan’s picture

It is not in the node.tpl.php that I am trying to access them, it is within the node body itself.

Thanks to efolia for your response, but I already new that I could use that method (though you weren't to know). Extracting from arg() has a couple of disadvantages:-

- Seems a clumsy work aroud. It seems strange that code in a node body has no direct access to, or even a direct reference to, it's own node data or node ID number.

- Would not work in a teaser - though I don't need it to for my current purpose.

Regards

John Bryan
www.ALT2.com
Application Integration Specialists
Tel: UK 08700 3456-31

efolia’s picture

Because the $node object is already processed (and split into different variables that are already theme()'d), it is not available at the page level. Since the said $node object is already in the cache, there is no performance penalty in using this 'workaround.' Indeed, it seems a bit ackward and counter-intuitive at first, but it is a consequence of Drupal's architecture where everything (the user sees) is a node. If it makes your programmer's mind feel more at ease (as it did me), you can write a simple function that gets the nid or even the whole $node object by putting your own code ('this_node()' or something similar) in template.tpl.php.

efolia

John Bryan’s picture

node_load_current()

In the "Helpers" module http://drupal.org/node/213579

I haven't tested it yet but sounds just the job.

ALSO

The "Token" module http://drupal.org/project/token

e.g.
[nid] Node ID
[type] Node type
[type-name] Node type (user-friendly version)
[title] Node title

Regards

John Bryan
www.ALT2.com
Application Integration Specialists
Tel: UK 08700 3456-31