How to get node id on hook_page_build(&$page) I wrote a hook like so:

function john_page_build(&amp;$page)
{
  $akeys = array_keys($page['content']['system_main']['nodes']);
  print $akeys[0];
}


On pages with urls like http://mysite.com/page/43, it will print the node id for that page.  However, on a page with url like http://mysite.co/page/43/done?sid=2, drupal gives a warning saying that $page['content']['system_main']['nodes'] does not exist.

Is there a consistent way to get the node id WITHOUT parsing the url?
Still comes through the URL indirectly, but this is the easiest way to get it: menu_get_object.

&lt;?
if ($node = menu_get_object()) {
 $nid = $node-&gt;nid;
}
?&gt;


Note that if there is a node, it was already loaded by the menu system anyway and this is loaded from the static cache.

Refer this discussion for more detail.