1 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. Defaults to node. These appear in hook_menu definitions as %type. Core provides contact, filter_format, menu, menu_link, node, taxonomy_vocabulary, user. See the relevant {$type}_load functions.

$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.

Return value

Entity|stdClass|array|NULL: The loaded object affiliated with the menu item position given. This can be nearly any kind of data (even an array in some cases). If no item is found at the given position, NULL is returned.

Related topics

File

core/includes/menu.inc, line 1040
API for the Backdrop 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];
  }
  return NULL;
}