Hopefully this is something easy (though search has not helped me much) - how to get a $node object given a drupal path?

say I have something like "node/231" or its alias "alias-for-node-231".

I would like to find out the type of that node, so need the $node object - which Drupal API function can give the $node for a path? Or is this not possible?

Comments

add1sun’s picture

If you are on the node and in the theme layer then you already have the $node object available to you. If you want to load the node based on the nid from a path, then you can use node_load() and pass in the nid. You can grab things from the Drupal path (node/1234) by using arg().

)nce you have the $node object you can just use $node->type.

Note: my links above are to the D6 version of the functions.

Lullabot loves you | Be a Ninja, join the Drupal Dojo

Drupalize.Me, The best Drupal training, available all the time, anywhere!

bwooster47’s picture

Thanks - that should solve the problem.

So I can do a drupal_get_normal_path and then a node_load to get node type, and even title.

Looking at the code for node_load - it seems overkill for just getting node type and title.
For example, instead of a node_load, would it be preferable (in terms of system load) to just do something like this:

    $node = db_fetch_object(db_query('SELECT n.nid, n.vid, n.type, r.title FROM {node} n INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE n.nid = %d', $nid));

The whole problem will look like this:
Given a list of strings that are nodes or aliases to nodes, for example: "node/1", "node/11", "node/34", I need to get the type and title of each of these. Do not need anything else.
So, is it faster to do node_load on these three, or to do the db_fetch_object?
If the node is cached, then node_load may be faster, but if not, then clearly the SELECT will be faster.

Any pros/cons to each approach?

[This thread should also solve (partially) the other problem I just noticed in my recent posts: How to get title given a path or alias? - will solve it for Drupal node paths, but not handle other types of Drupal paths.]

criznach’s picture

node_load is probably fine in most cases. How many nodes are you loading? Do you currently have a performance problem and have you identified the cause to be node_load? If not, then spend your energy elsewhere until it becomes an issue. node_load could easily be replaced down the road if you find that it is a bottleneck. Many people have said that premature optimization is the root of all evil.

benmirkhah’s picture

$path = drupal_get_normal_path('my/fancy/path/node');
$node = menu_get_object('node', 1, $path);

tamerzg’s picture

To get a node object from path you can use something like this:

//First we must get a system path:
$system_path = drupal_lookup_path('source', $path);

//Then load the menu item based on system path:
$menu_item = menu_get_item($system_path);
if($menu_item && isset($menu_item['page_arguments'][0])) {
//And here we get the full node object:  
$node = $menu_item['page_arguments'][0]
}

--------------
zoubi.me