How can I get at the node object from a PHP page? I've tried using node_load and arg(1) but that only works if I'm actually viewing the page, not when listed as a teaser.

Comments

nevets’s picture

The node object ($node) is only defined in page.tpl.php and the trick of looking at arg(0) == 'node' and using arg(1) as the nid only works when viewing a single node on the page. So it would probably help if you explained what you are trying to do and people would be better able to help you.

ninetwenty’s picture

I need to be able to get the current node id and pull some related data from a table and display this in the body. Looks like I can't do it as there is no way to get the real current node.

nevets’s picture

You can write a module that implement the nodeapi hook which for your purposes would be called when a node is viewed ($op == 'view') at which point you could add to the body of the node.

Second approach is to handle it in node.tpl.php and add the information when the node is displayed.

ninetwenty’s picture

So a possible solution would be to use a regular expression to replace some marker text in the body with the content I want? For example:

function mymodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'view':
      $node->body = preg_replace('\MARKER\', 'replacement content', $node->body);
      $node->teaser = preg_replace('\MARKER\', 'replacement content', $node->teaser);
      break;
  }
}
nevets’s picture

In this case because you want to replace a marker you could also use the filter hook. The filter hook would be useful if you wanted to be able to specify options/parameters for what should be included.

ninetwenty’s picture

And the filter hook has access to the node object? If it does that would be perfect.

nevets’s picture

Actually the body/teaser (depending on context), it does not have access to the complete node (or even "know" it is being used to filter a nodes content.