6.x menu.inc menu_get_object($type = 'node', $position = 1, $path = NULL)
7.x menu.inc menu_get_object($type = 'node', $position = 1, $path = NULL)

Gets a loaded object from a router item.

menu_get_object() provides access to objects loaded by the current router item. For example, on the page node/%node, the router loads the %node object, and calling menu_get_object() will return that. Normally, it is necessary to specify the type of object referenced, however node is the default. The following example tests to see whether the node being displayed is of the "story" content type:

$node = menu_get_object();
$story = $node->type == 'story';


$type: Type of the object. These appear in hook_menu definitions as %type. Core provides aggregator_feed, aggregator_category, contact, filter_format, forum_term, menu, menu_link, node, taxonomy_vocabulary, user. See the relevant {$type}_load function for more on each. Defaults to node.

$position: The position of the object in the path, where the first path segment is 0. For node/%node, the position of %node is 1, but for comment/reply/%node, it's 2. Defaults to 1.

$path: See menu_get_item() for more on this. Defaults to the current path.

Related topics

3 calls to menu_get_object()
node_block_list_alter in modules/node/node.module
Implements hook_block_list_alter().
node_is_page in modules/node/node.module
Returns whether the current page is the full page view of the passed-in node.
taxonomy_term_is_page in modules/taxonomy/taxonomy.module
Returns whether the current page is the page of the passed-in term.


includes/menu.inc, line 981
API for the Drupal menu system.


function menu_get_object($type = 'node', $position = 1, $path = NULL) {
  $router_item = menu_get_item($path);
  if (isset($router_item['load_functions'][$position]) && !empty($router_item['map'][$position]) && $router_item['load_functions'][$position] == $type . '_load') {
    return $router_item['map'][$position];


lazysoundsystem’s picture

Note that taxonomy_term is not listed in the $type parameter. This should work unless the Views module is enabled.

Views defines its own callback for the taxonomy path taxonomy/term/% (instead of taxonomy/term/%taxonomy_term - see http://drupal.org/node/1236840#comment-4820598), so when the menu_router table is built, there is no load_function for taxonomy terms.

This appears to be the case whether the 'Taxonomy term' view is enabled or not.

lmeurs’s picture

According to https://drupal.org/node/1468006#comment-6243526 the next line works for taxonomy:

$term = menu_get_object('taxonomy_term', 2);

I tested it with succes on Drupal 7.22 and Views 7.x-3.7, with the Taxonomy term view enabled or disabled.

lmeurs’s picture

Nope, I have to stand corrected. The Views module seemed to not rebuild the menu cache after enabling (or disabling) the taxonomy/term/% view, so the load function callback still was taxonomy_term_load. After manually clearing the menu cache the load function callback became views_arg_load.

My conclusion so far: after enabling the taxonomy/term/% view and clearing the menu cache, I could not get menu_get_item() to load the current taxonomy term.

mxh’s picture

Yup, you're correct because Views will override the menu entry for taxonomy/term/% when a view is activated which has the same path.

The same applies for every other path regarding Views, e.g. setting up a view page with path node/% will override the menu entry of core module node. Then, menu_get_object() also won't work for nodes.

As a conclusion, beware of using menu_get_object() with blue eyes. Always add some checks and if it fails, try to get the object by other ways, for example:

$term = menu_get_object('taxonomy_term', 2);
if (!isset($term->tid)) {
  // Try to get it without menu_get_object().
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && arg(2)) {
    $term = taxonomy_term_load(arg(2));
  // Further checks...
mxh’s picture

I need to add to my comment above that you shold beware of that the alternative ways of fetcing the object don't automatically respect access restrictions.

For example, when you had no luck getting the node via menu_get_object() and you're trying to get it with node_load(arg(1)) or similar, the node gets loaded without access check.
You have to make sure on your own that no access bypass is happening. See node_access() for this case.

MKorostoff’s picture

colan’s picture

leewoodman’s picture

my user profile URL's look like www.mysite.com/username
To access the UID when browsing the user profile i use this in my custom module
$user = menu_get_object ( 'user', 1 ); // Where 1 is the position in the URL
$uid = $user->uid;

Mykola Dolynskyi’s picture

function entity_by_path($path) {
    $router_item = menu_get_item($path);

    $ent_sug_array = array();
    foreach ($router_item['map'] as $key => $value) {
        if(is_object($value) && isset($value->type)) {
           $ent_sug_array[$key] =  $value;
    $ent_sug = false;
    if(count($ent_sug_array) > 1) {
        $lf_sug = false;
        if(isset($router_item['load_functions'])) {
            if(count($router_item['load_functions']) == 1) {
                $lf_sug = key($router_item['load_functions']);
        if($lf_sug !== false) {
            $ent_sug = is_object($router_item['map'][$lg_sug] && isset($router_item['map'][$lg_sug]->type)) ? $router_item['map'][$lg_sug] : $ent_sug;
    } elseif(count($ent_sug_array) == 1) {
        $ent_sug = reset($ent_sug_array);
    if(!empty($ent_sug)) {
        $bundle = $ent_sug->type;
        $ent_type = method_exists($ent_sug, 'entityType') ? $ent_sug->entityType() : false; //eck case
        if(empty($ent_type)) {
            $aclass = get_class($ent_sug);
            if($aclass == 'stdClass') {
                //node, user, core entity case
                if(isset($router_item['load_functions'])) {
                    $lf = reset($router_item['load_functions']);
                    $ei = entity_get_info();
                    foreach ($ei as $key => $value) {
                        if(empty('load hook'))
                        if($lf == $value['load hook']) {
                            $ent_type = $key;
                //custom entity case
                $ent_type = $aclass;
        return array('entity'=>$ent_sug, 'type'=>$ent_type, 'bundle'=>$bundle);
    return false;
welly’s picture

This doesn't work for taxonomy terms when you have Views enabled.

wajdigharbi’s picture

Doesn't work for me too when i use it for taxonomy terms with views enabled