hook_menu

Хочешь помочь с переводом? Это очень просто и быстро. Лишь зарегистрируйся, и можешь тут же начать переводить.

developer/hooks/core.php, строка 760

Версии
5
hook_menu($may_cache)
6
hook_menu()

Определяет пункты меню и обратный вызов страниц.

Этот хук разрешает модулям регистрировать пути, которые определяют запросы, подлежащие обработке. В зависимости от типа регистрации, запрошенного для каждого пути, ссылка помещается в блоке навигации и/или пункт появляется в меню административной страницы (q=admin/menu).

Drupal will call this hook twice: once with $may_cache set to TRUE, and once with it set to FALSE. Therefore, each menu item should be registered when $may_cache is either TRUE or FALSE, not both times. Setting a menu item twice will result in unspecified behavior.

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.

For a detailed usage example, see page_example.module.

Параметры

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

Возвращаемое значение

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.

Связанные темы

▾ 2 функции вызывают hook_menu()

_menu_append_contextual_items in includes/menu.inc
Account for menu items that are only defined at certain paths, so will not be cached.
_menu_build in includes/menu.inc
Build the menu by querying both modules and the database.

Код

<?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;
}
?>
Войдите или зарегистрируйтесь, чтобы получить возможность отправлять комментарии

Вход в систему