[development] node object in hook_block?

Robrecht Jacques robrechtj+drupal at gmail.com
Sat Jul 1 09:50:40 UTC 2006


On 7/1/06, Augustin (a.k.a. Beginner) <drupal.beginner at wechange.org> wrote:

> I see many implementations of hook_block who declare
>    global $user
> but none with
>    global $node
>
> I tried it but $node is still not set, so I cannot access the node object
> directly.


That is because there is no global $node. There is a global $user viewing
the page, but not every page has a $node that it needs to display (eg all
kinds of "listing" pages or the admin pages etc). Blocks can appear on any
page, not only node-pages. So hook_block() doesn't have direct access to a
$node variable.

Note that the functions that need $node always have a $node parameter
passed. There is no global $node that I'm aware of (with maybe the exception
of theming, but that is then handled by phptemplate).

the solution would be to do like in book_hook() and check on arg():
>     if (arg(0) == 'node' && is_numeric(arg(1)))
> but it looks like a hack and for what I have in mind, it makes things more
> complicated.


I think this is the correct way of doing this. And I don't really see how
this makes things more complicated. The test checks if we are on a
node-page, and if you are you can load the node needed. node.module makes
sure the node is loaded only once, so there is no overload either.

Isn't there a possibility to have directly access to the $node object?
> How?


Don't think so.

Kind regards,
Robrecht
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20060701/f48f5276/attachment.htm


More information about the development mailing list