I thought this would be really easy to find, but could find a reference, so was hoping someone could tell me how I can retrieve the $node obejct if I know the node's id.

Here's what I'm doing: I have the category module installed and want to call one of it's functions from within a block. Here's the function that I will be called, as defined in modules/category/category_display/category_display.module on line 312.

/**
 * Returns an HTML nested list (wrapped in a menu-class div) representing
 * the category nodes as a tree.
 */
function category_display_toc($node) {
  $depth = $node->toc_depth;
  if ($depth < 0) {
    $depth = 100;
  }
  if ($toc = category_display_toc_recurse($node->nid, $depth, $node)) {
    return '<ul class="menu">'. $toc .'</ul>';
  }
}

I am thinking that if I put the following in a block, and have a way to get the node object from the nid, then the block will display the TOC for the "book"/container who's node I specify in $nid.

<?
$nid = 12;
$node_of_menu = ??????????????????;
category_display_toc($node_of_menu);
?>

What do I replace the ???????????? with to make this work? Is there a predefined function I can call to accomplish this? I would imagine there would be ....

Thanks a bunch,

Michael
www.webemulsion.com

Comments

budda’s picture

node_load(array('nid' => $nid));

--
Ixis (UK) providing Drupal consultancy and Drupal theme design.

pbarnett’s picture

http://api.drupal.org/api/4.7/function/node_load

You can just call $node = node_load($nid); to get a fully-loaded node object given the node id.

Pete.

OpenChimp’s picture

Awesome! Thank you guys.

My Category pages now have a book style TOC block.

Here's the snippet I used. Just paste this in a new block and specify PHP as the input format and tell it which pages you want the block to display on. Using path_auto makes it easy to generate pages that reflect the menu system, which allows you to specify the first part of the url as the options for where the block should display, such as specifying "programs*" to have the Programs TOC display on all Program pages.

<?
$nid = 10;
$node_for_TOC = node_load($nid);
print category_display_toc($node_for_TOC);
?>

Unlike a menu, it will inheret the features you set for the TOC in the Category module options, such as displaying a count of associated nodes.

Michael
www.webemulsion.com

odsamuels’s picture

Greetings,

I've been searching and this is pretty close to a problem I am trying to solve. With my problem, I am trying to get the id of a node in a block, but for some reason it just doesn't work. In the post from MikeyLikesIt he seems to have the number of the $nid already - "$nid = 12;" but I want it to b dependent on the current node... as in dynamically. How can I do this?

CentEur’s picture

drubb’s picture

Just try $node instead of $node->nid or $nid.

Good Luck!

bramick’s picture

Used comments on this page to solve my solution too, thanks:

http://drupal.org/node/104601#comment-841093