[development] Context in Drupal 6

Karoly Negyesi karoly at negyesi.net
Sat Dec 8 10:32:09 UTC 2007


Hi,

Where should I put this in the handbook? I was running through feature
requests, closing them or moving them to 7.x and found one titled
"Create a system for storing the 'page context' ". Curiously, Drupal
6.x got this. Let me elaborate.

In old times, if you wanted to fire on node view, then you had
something like if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2))
$node = node_load(arg(1)) ....

This good but not quite. What if someone adds a link to node/1/foo and
acts on foo on nodeapi? The menu system will fall back to node/1...
How can you discern between node/1/foo and node/1/edit ?

Now you can do this: $router_item = menu_get_item(); if
($router_item['path'] == 'node/%' && $router_item['access']) $node =
$router_item['map'][1] ...

Now, this piece will, indeed, fire only when you are viewing a node.
The additional bonus is that if you are access denied, then the code
does not fire. The speed is approximately the same, as menu_get_item
is also static cached as node_load. Even, it'll be a very little bit
faster because you do not call arg many times. And of course, much
more readable -- once you know that map keeps the parts of the path
sent to the load functions. And if you act on something which is not
statically cached -- practically anything else than a node -- then you
will be much faster.

Regards,

NK


More information about the development mailing list