menu_get_object

Versions
6 – 7
menu_get_object($type = 'node', $position = 1, $path = NULL)

Get 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:

<?php

$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

▾ 11 functions call menu_get_object()

block_block_info_alter in modules/block/block.module
Implement hook_block_info_alter().
blog_view in modules/blog/blog.module
Implement hook_view().
book_block_view in modules/book/book.module
Implement hook_block_view().
book_page_alter in modules/book/book.module
Implement hook_page_alter().
comment_node_view in modules/comment/comment.module
Implement hook_node_view().
forum_node_view in modules/forum/forum.module
Implement hook_node_view().
hook_page_build in modules/system/system.api.php
Add elements to a page before it is rendered.
hook_view in modules/node/node.api.php
Display a node.
template_preprocess_html in includes/theme.inc
Preprocess variables for html.tpl.php
template_preprocess_node in modules/node/node.module
Process variables for node.tpl.php
template_preprocess_page in includes/theme.inc
Preprocess variables for page.tpl.php

Code

includes/menu.inc, line 840

<?php
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];
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.