Hi all,

Does anyone know how I can find out what the node id that corresponds to a specific Drupal path?

I've been searching for quite a while for an answer to this and I'm coming up empty, it seems as though there should be a fairly simple way to do this (with node_load, I was thinking, but, I've had little luck with that), but, as I said, no luck!

Thanks.

(I suppose not all paths will correspond to nodes, but, in my case the paths I am interested in will be... I think... I'm pretty new to this stuff)

Comments

go to administer -> content
filter for the node you want
hover over the title link
there you will find the nid

Thanks, but I meant programmatically from within a drupal module...

So do you mean a path or an alias? Nodes always have paths of the form node/{nid} where {nid} is the node id. If you mean looking up the path associated with an alias see http://api.drupal.org/api/function/drupal_lookup_path/6

I must mean an alias... I'm pulling the aliases from the {menu} table in my drupal database, and some of the time the paths/aliases are in the form /node/{nid}, but, most of the time they're just the aliases.

I've tried the function you've suggested, but, it doesn't seem to be working for me.

Here is my code:

$action = 'source';
if ($sys_path = drupal_lookup_path($action, $path = $found_path->path)) {
drupal_set_message($sys_path . '.');
} else {
drupal_set_message('No system path found.');
}

$found_path->path certainly contains an alias, but I am still getting the 'No system path found.' message. (I'm running Drupal 5.x)

Thanks for the help! I feel silly having so much trouble with what should be a very simple matter.

If you want to get the node id of the aliased path in the fly, you can use

drupal_get_normal_path($path);

It will return something ie: node/3, which 3 is the nid if. But if you want to get the nid of the current state path, arg(1) will always be a node id, assuming you are in the node page.

I think my issue may actually be elsewhere in my code... I've been testing on admin/* pages, which are perhaps not nodes at all... both of these methods will likely work on an actual nodes.

Sorry for the trouble, this will be very useful when I'm actually testing on real nodes!
Thanks! :P

Correct, paths under admin are not typically nodes.

Admin pages are never nodes. Nodes are a specific type of data in Drupal. They really form the core of the Drupal system. Nodes are data that users add, using the 'create content' menu. Pages generated programatically are not nodes (unless someone configures a module to generate nodes, though I've not seen such a module, one could and may very well exist). There are other types of user generated data that are also not nodes, for example private messages in the privatemsg module generates data that isn't nodes, but these are more of an exception than a rule. For the most part, user generated content will be in the form of a node.

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

Try this:

global $base_url;

$url= "http://www.domain.com/baby/1-week-pregnant";

$alias = str_ireplace("$base_url/",'',$url);

$normal_path=drupal_get_normal_path($alias);

$nid=str_ireplace("node/",'',$normal_path);

echo ("nid=$nid");

owolawi

For the current path it is easier to use

if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $nid = arg(1);
}