I want to get the node ID to load the nid fields.
How can I get the node ID from the current URL using this function?


That's not what drupal_get_path() is for; it's for getting the internal router path for a URL given a path alias, and vice versa. It also allows you to wipe aliases, but it's not a good way to try to get the current page's node ID.

What you're looking for is the arg() function, which simply splits the current router path (the value contained in $_GET['q'], which for a node page will be, for example, node/1).

// With a router path of node/123
$entity_type = arg(0); // $entity_type is equal to 'node'

$nid = arg(1); // $nid is equal to 123
  • good answer but can you tell me who can i get the nid of some field value. i mean i have reference field and i want to get the nid if this field – abd Apr 5 '12 at 12:48
  • 1
    It would be something like $referenced_node = node_load($node->field_name_of_reference_field[$node->language][0]['nid']));. That will get you the referenced node object, from which you can then get the field value. That's if you're using the references module, it might be something different if you're using entity reference/relation – Clive Apr 5 '12 at 12:51
  • thanks clive but i put this code in token field and the page of node became white when refreshed <?php $nid = arg(1); print $nid; $node= node_load($nid); $newnid=$node->field_idnumber['und'][0]['nid']; $referenced_node = node_load($newnid); ?> – abd Apr 5 '12 at 13:17
  • I'm afraid I can't comment on PHP code in token fields, I've never used them. The code you've posted is 100% valid Drupal code though, if it's causing an error then the problem is somewhere else. Are you using the Token Field module by any chance? It is in dev for Drupal 7 so if you are I'd imagine that's what the problem is – Clive Apr 5 '12 at 13:20

As Clive said, that is not the purpose of drupal_lookup_path(). If you want to get the node ID associated with a path, you just use code similar to the following one.

if ($node = menu_get_object('node', 1, $path)) {
  // $node->nid is the node ID.

That code is for the case you are interested in a specific path, and not the current one. If you are looking for the node associate to the current path, then the code can be simply written as the following one.

if ($node = menu_get_object()) {
  // $node->nid is the node ID.

"node", and 1 are the default values for the first two parameters, and you can avoid passing any parameters if you are interested to get the node object associated with the current path.

Consider that:

  • The function returns FALSE if the current path (or the path passed to the function) doesn't have any node associated, for example because the path is user/1.
  • If there is a node associated with the path, the node object is the one returned from node_load(), which then uses entity_load() to get it.
  • Differently from the approach followed using arg(), menu_get_object() always return the right node object. Suppose the site have enabled node revisions; if you would use the following code, you would always get the same node object, even if the path is (for example) node/8/revisions/14.

    if (arg(0) == 'node') {
      $node = node_load(arg(1));

    This is not a case limited to node revisions being enabled. For example, there could be a module that defines a menu callback for paths like node/8/linked/12, and that would show the node page for the node with the ID equal to, e.g., 23. Using the code calling arg() I shown, you would load the node that is not associated with the current page, which is not what happens with menu_get_object().

Once you have the node object, you can access to the fields loaded with it, or you can pass the node object to any entity function, to get more information about the node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.