function menu_get_object

You are here

7 menu.inc menu_get_object($type = 'node', $position = 1, $path = NULL)
6 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';

Parameters

$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

8 calls to menu_get_object()
book_block_view in modules/book/book.module
Implements hook_block_view().
book_page_alter in modules/book/book.module
Implements hook_page_alter().
hook_page_build in modules/system/system.api.php
Add elements to a page before it is rendered.
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.

... See full list

File

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

Code

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];
  }
}

Comments

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.

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.

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.