By bwooster47 on
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
node_load
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!
what is faster - db_query, or node_load
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:
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.]
node_load is probably fine
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.
http://www.trailheadinteractive.com
Load a node using its path
$path = drupal_get_normal_path('my/fancy/path/node');
$node = menu_get_object('node', 1, $path);
To get a node object from
To get a node object from path you can use something like this:
--------------
zoubi.me