Getting the Node NID within Modules

Public

Loading the Node Object within a Module

</> CopyGet raw version
php
  1. /*
  2.  * You can use $node = menu_get_object(); to load the $node object to be used within your functions in any module.
  3.  * abdul@wakensys.com
  4.  */
  5.  
  6. function myModule_anything() {
  7.  
  8. $node = menu_get_object();
  9. $nid = $node->nid;
  10.  
  11. ..... // do something
  12. }

Comments

timonweb's picture

Note, this works only when function is called on a 'node/*' page.

carks's picture

a better way

  1. $node = menu_get_object($type = 'node');
abdulqadir028's picture

The Node is default and yeah it's only if we are to do something with the current node. :)

Anonymous's picture

good old method.. but will this work for router element like autocomplete?