Context in Drupal 6
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
Karoly Negyesi wrote:
Now you can do this: $router_item = menu_get_item(); if ($router_item['path'] == 'node/%' && $router_item['access']) $node = $router_item['map'][1] ...
This isn't quite the same. One thing I've realized while writing the context system in Panels 2 is that arguments are only one source of context. I also have relationships, which are a very important source of context, and built-in contexts, which are maybe not quite as important in this scenario. So it's a half-way step, but it is only half way, and it still requires the context consumer to have entirely too much knowledge of the construction of the URL, IMO.
Following up this, phptemplate_preprocess_page was loading the node with if ((arg(0) == 'node') && is_numeric(arg(1))) { $variables['node'] = node_load(arg(1)); } but on node/revisions/48/view this is wrong. We now have: if ($node = menu_get_object()) { $variables['node'] = $node; } Here is another example for this function: if ((arg(0) == 'user') && is_numeric(arg(1))) { $account = user_load(arg(1)); } Gets replaced with $account = menu_get_object('user'); I know I should not change API this late but this function was a necessaery bugfix and hardwiring it for nodes looked a bad decision. See the documentation of this function on this new and IMO very useful -- but admittely limited -- context getter. Regards, NK
participants (2)
-
Earl Miles -
Karoly Negyesi