Community Documentation

hook_menu

5 core.php hook_menu($may_cache)
6 core.php hook_menu()
7 system.api.php hook_menu()
8 system.api.php hook_menu()

Define menu items and page callbacks.

This hook enables modules to register paths, which determines whose requests are to be handled. Depending on the type of registration requested by each path, a link is placed in the the navigation block and/or an item appears in the menu administration page (q=admin/menu).

This hook is also a good place to put code which should run exactly once per page view. Put it in an if (!may_cache) block.

Parameters

$may_cache: A boolean indicating whether cacheable menu items should be returned. The menu cache is per-user, so items can be cached so long as they are not dependent on the user's current location. See the local task definitions in node_menu() for an example of uncacheable menu items.

Return value

An array of menu items. Each menu item is an associative array that may contain the following key-value pairs:

  • "path": Required. The path to link to when the user selects the item.
  • "title": Required. The translated title of the menu item.
  • "callback": The function to call to display a web page when the user visits the path. If omitted, the parent menu item's callback will be used instead.
  • "callback arguments": An array of arguments to pass to the callback function.
  • "access": A boolean value that determines whether the user has access rights to this menu item. Usually determined by a call to user_access(). If omitted and "callback" is also absent, the access rights of the parent menu item will be used instead.
  • "weight": An integer that determines relative position of items in the menu; higher-weighted items sink. Defaults to 0. When in doubt, leave this alone; the default alphabetical order is usually best.
  • "type": A bitmask of flags describing properties of the menu item. Many shortcut bitmasks are provided as constants in menu.inc:

    • MENU_NORMAL_ITEM: Normal menu items show up in the menu tree and can be moved/hidden by the administrator.
    • MENU_ITEM_GROUPING: Item groupings are used for pages like "node/add" that simply list subpages to visit.
    • MENU_CALLBACK: Callbacks simply register a path so that the correct function is fired when the URL is accessed.
    • MENU_DYNAMIC_ITEM: Dynamic menu items change frequently, and so should not be stored in the database for administrative customization.
    • MENU_SUGGESTED_ITEM: Modules may "suggest" menu items that the administrator may enable.
    • MENU_LOCAL_TASK: Local tasks are rendered as tabs by default.
    • MENU_DEFAULT_LOCAL_TASK: Every set of local tasks should provide one "default" task, that links to the same path as its parent when clicked.

    If the "type" key is omitted, MENU_NORMAL_ITEM is assumed.

For a detailed usage example, see page_example.module.

Related topics

▾ 38 functions implement hook_menu()

aggregator_menu in modules/aggregator.module
Implementation of hook_menu().
archive_menu in modules/archive.module
Implementation of hook_menu().
block_menu in modules/block.module
Implementation of hook_menu().
blogapi_menu in modules/blogapi.module
blog_menu in modules/blog.module
Implementation of hook_menu().
book_menu in modules/book.module
Implementation of hook_menu().
comment_menu in modules/comment.module
Implementation of hook_menu().
contact_menu in modules/contact.module
Implementation of hook_menu().
drupal_menu in modules/drupal.module
Implementation of hook_menu().
fileupload_menu in developer/examples/fileupload.module
Implementation of hook_menu.
filter_menu in modules/filter.module
Implementation of hook_menu().
forum_menu in modules/forum.module
Implementation of hook_menu().
help_menu in modules/help.module
Implementation of hook_menu().
legacy_menu in modules/legacy.module
Implementation of hook_menu().
locale_menu in modules/locale.module
Implementation of hook_menu().
MENU_CUSTOM_MENU in includes/menu.inc
Custom menus are those defined by the administrator. Reserved for internal use; do not return from hook_menu() implementations.
menu_get_menu in includes/menu.inc
Return the menu data structure.
menu_in_active_trail_in_submenu in includes/menu.inc
Returns true when the menu item is in the active trail within a specific subsection of the menu tree.
menu_menu in modules/menu.module
Implementation of hook_menu().
multipage_form_example_menu in developer/examples/multipage_form_example.module
Implementation of hook_menu().
node_example_menu in developer/examples/node_example.module
Implementation of hook_menu().
node_menu in modules/node.module
Implementation of hook_menu().
page_example_menu in developer/examples/page_example.module
Implementation of hook_menu().
page_menu in modules/page.module
Implementation of hook_menu().
path_menu in modules/path.module
Implementation of hook_menu().
poll_menu in modules/poll.module
Implementation of hook_menu().
profile_menu in modules/profile.module
Implementation of hook_menu().
search_menu in modules/search.module
Implementation of hook_menu().
statistics_menu in modules/statistics.module
Implementation of hook_menu().
story_menu in modules/story.module
Implementation of hook_menu().
system_menu in modules/system.module
Implementation of hook_menu().
taxonomy_menu in modules/taxonomy.module
Implementation of hook_menu().
theme_submenu in includes/theme.inc
Return a themed submenu, typically displayed under the tabs.
tracker_menu in modules/tracker.module
Implementation of hook_menu().
upload_menu in modules/upload.module
Implementation of hook_menu().
user_menu in modules/user.module
Implementation of hook_menu().
watchdog_menu in modules/watchdog.module
Implementation of hook_menu().
_menu_get_active_trail_in_submenu in includes/menu.inc
Find the active trail through a specific subsection of the menu tree.

File

developer/hooks/core.php, line 612
These are the hooks that are invoked by the Drupal core.

Code

<?php
function hook_menu($may_cache) {
  global $user;
  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'node/add/blog', 
      'title' => t('blog entry'), 
      'access' => user_access('maintain personal blog'),
    );
    $items[] = array(
      'path' => 'blog', 
      'title' => t('blogs'), 
      'callback' => 'blog_page', 
      'access' => user_access('access content'), 
      'type' => MENU_SUGGESTED_ITEM,
    );
    $items[] = array(
      'path' => 'blog/' . $user->uid, 
      'title' => t('my blog'), 
      'access' => user_access('maintain personal blog'), 
      'type' => MENU_DYNAMIC_ITEM,
    );
    $items[] = array(
      'path' => 'blog/feed', 
      'title' => t('RSS feed'), 
      'callback' => 'blog_feed', 
      'access' => user_access('access content'), 
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}
?>
Login or register to post comments