- <?php
- * @file
- * Hook implementations for the Menu Example module.
- */
-
- * @defgroup menu_example Example: Menu
- * @ingroup examples
- * @{
- * This example demonstrates how to use the menu API.
- *
- * The Page Example module is another module that show how to use the menu
- * system, as well as how to use menu arguments to generate pages.
- *
- * @see hook_menu()
- * @see hook_menu_alter()
- * @see hook_menu_link_alter()
- * @see page_example
- * @see page_example_menu()
- */
-
- * Implements hook_menu().
- *
- * A simple example which defines a page callback and a menu entry.
- */
- function menu_example_menu() {
-
-
-
-
- $items['examples/menu_example'] = array(
-
-
-
-
-
-
- 'title' => 'Menu Example',
-
-
-
- 'description' => 'Simplest possible menu type, and the parent menu entry for others',
-
-
- 'page callback' => '_menu_example_basic_instructions',
-
-
-
- 'page arguments' => array(t('This page is displayed by the simplest (and base) menu example. Note that the title of the page is the same as the link title. You can also <a href="!link">visit a similar page with no menu link</a>. Also, note that there is a hook_menu_alter() example that has changed the path of one of the menu items.', array('!link' => url('examples/menu_example/path_only')))),
-
-
-
-
-
- 'access callback' => TRUE,
-
-
-
-
-
-
-
-
-
- 'expanded' => TRUE,
- );
-
-
-
- $items['examples/menu_example_alternate_menu'] = array(
- 'title' => 'Menu Example: Menu in alternate menu',
-
-
- 'menu_name' => 'user-menu',
-
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('This will be in the Primary Links menu instead of the default Navigation menu')),
- 'access callback' => TRUE,
- );
-
-
-
-
-
- $items['examples/menu_example/permissioned'] = array(
- 'title' => 'Permissioned Example',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('A menu item that requires the "access protected menu example" permission is at <a href="!link">examples/menu_example/permissioned/controlled</a>', array('!link' => url('examples/menu_example/permissioned/controlled')))),
- 'access callback' => TRUE,
- 'expanded' => TRUE,
- );
-
-
- $items['examples/menu_example/permissioned/controlled'] = array(
-
-
- 'title' => 'Permissioned Menu Item',
- 'description' => 'This menu entry will not appear and the page will not be accessible without the "access protected menu example" permission.',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('This menu entry will not show and the page will not be accessible without the "access protected menu example" permission.')),
-
-
-
-
-
-
-
-
-
-
- 'access arguments' => array('access protected menu example'),
-
-
-
- 'weight' => 10,
- );
-
-
- * We will define our own "access callback" function. We'll use
- * menu_example_custom_access() rather than the default user_access().
- *
- * The function takes a "role" of the user as an argument.
- */
- $items['examples/menu_example/custom_access'] = array(
- 'title' => 'Custom Access Example',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('A menu item that requires the user to posess a role of "authenticated user" is at <a href="!link">examples/menu_example/custom_access/page</a>', array('!link' => url('examples/menu_example/custom_access/page')))),
- 'access callback' => TRUE,
- 'expanded' => TRUE,
- 'weight' => -5,
- );
-
- $items['examples/menu_example/custom_access/page'] = array(
- 'title' => 'Custom Access Menu Item',
- 'description' => 'This menu entry will not show and the page will not be accessible without the user being an "authenticated user".',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('This menu entry will not be visible and access will result in a 403 error unless the user has the "authenticated user" role. This is accomplished with a custom access callback.')),
- 'access callback' => 'menu_example_custom_access',
- 'access arguments' => array('authenticated'),
- );
-
-
-
-
-
-
-
-
- $items['examples/menu_example/path_only'] = array(
- 'title' => 'MENU_CALLBACK example',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('A menu entry with no menu link (MENU_CALLBACK) is at <a href="!link">!link</a>', array('!link' => url('examples/menu_example/path_only/callback')))),
- 'access callback' => TRUE,
- 'weight' => 20,
- );
- $items['examples/menu_example/path_only/callback'] = array(
-
-
-
- 'type' => MENU_CALLBACK,
-
-
-
- 'title' => 'Callback Only',
-
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('The menu entry for this page is of type MENU_CALLBACK, so it provides only a path but not a link in the menu links, but it is the same in every other way to the simplest example.')),
- 'access callback' => TRUE,
- );
-
-
-
-
-
-
-
-
- $items['examples/menu_example/tabs'] = array(
-
- 'title' => 'Tabs',
- 'description' => 'Shows how to create primary and secondary tabs',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('This is the "tabs" menu entry.')),
- 'access callback' => TRUE,
- 'weight' => 30,
- );
-
-
-
- $items['examples/menu_example/tabs/default'] = array(
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'title' => 'Default primary tab',
- 'weight' => 1,
- );
-
- foreach (array(t('second') => 2, t('third') => 3, t('fourth') => 4) as $tabname => $weight) {
- $items["examples/menu_example/tabs/$tabname"] = array(
- 'type' => MENU_LOCAL_TASK,
- 'title' => $tabname,
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('This is the tab "@tabname" in the "basic tabs" example', array('@tabname' => $tabname))),
- 'access callback' => TRUE,
-
-
-
- 'weight' => $weight,
- );
- }
-
-
-
-
- $items['examples/menu_example/tabs/default/first'] = array(
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'title' => 'Default secondary tab',
-
-
- );
- foreach (array(t('second'), t('third')) as $tabname) {
- $items["examples/menu_example/tabs/default/$tabname"] = array(
- 'type' => MENU_LOCAL_TASK,
- 'title' => $tabname,
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('This is the secondary tab "@tabname" in the "basic tabs" example "default" tab', array('@tabname' => $tabname))),
- 'access callback' => TRUE,
- );
- }
-
-
-
-
-
- $items['examples/menu_example/use_url_arguments'] = array(
- 'title' => 'Extra Arguments',
- 'description' => 'The page callback can use the arguments provided after the path used as key',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('This page demonstrates using arguments in the path (portions of the path after "menu_example/url_arguments". For example, access it with <a href="!link1">!link1</a> or <a href="!link2">!link2</a>).', array('!link1' => url('examples/menu_example/use_url_arguments/one/two'), '!link2' => url('examples/menu_example/use_url_arguments/firstarg/secondarg')))),
- 'access callback' => TRUE,
- 'weight' => 40,
- );
-
-
-
-
- $items['examples/menu_example/title_callbacks'] = array(
- 'title callback' => '_menu_example_simple_title_callback',
- 'title arguments' => array(t('Dynamic title: username=')),
- 'description' => 'The title of this menu item is dynamically generated',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('The menu title is dynamically changed by the title callback')),
- 'access callback' => TRUE,
- 'weight' => 50,
- );
-
-
-
-
-
-
-
-
-
-
-
-
- $items['examples/menu_example/placeholder_argument'] = array(
- 'title' => 'Placeholder Arguments',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('Demonstrate placeholders by visiting <a href="!link">examples/menu_example/placeholder_argument/3343/display</a>', array('!link' => url('examples/menu_example/placeholder_argument/3343/display')))),
- 'access callback' => TRUE,
- 'weight' => 60,
- );
-
-
- $items['examples/menu_example/placeholder_argument/%/display'] = array(
- 'title' => 'Placeholder Arguments',
- 'page callback' => '_menu_example_menu_page',
-
-
-
-
-
- 'page arguments' => array(3),
- 'access callback' => TRUE,
- );
-
-
-
-
-
-
-
-
-
-
- $items['examples/menu_example/default_arg/%menu_example_arg_optional'] = array(
- 'title' => 'Processed Placeholder Arguments',
- 'page callback' => '_menu_example_menu_page',
-
- 'page arguments' => array(3),
- 'access callback' => TRUE,
- 'weight' => 70,
- );
-
- $items['examples/menu_example/menu_original_path'] = array(
- 'title' => 'Menu path that will be altered by hook_menu_alter()',
- 'page callback' => '_menu_example_menu_page',
- 'page arguments' => array(t('This menu item was created strictly to allow the hook_menu_alter() function to have something to operate on. hook_menu defined the path as examples/menu_example/menu_original_path. The hook_menu_alter() changes it to examples/menu_example/menu_altered_path. You can try navigating to both paths and see what happens!')),
- 'access callback' => TRUE,
- 'weight' => 80,
- );
- foreach ($items as $path => $item) {
- if (!isset($item['menu_name'])) {
- $items[$path]['menu_name'] = 'menu-example-menu';
- }
- }
-
- return $items;
- }
-
- * Page callback for the simplest introduction menu entry.
- *
- * @param string $content
- * Some content passed in.
- */
- function _menu_example_basic_instructions($content = NULL) {
- $base_content = t(
- 'This is the base page of the Menu Example. To view the full range of example menu links created by Menu Example, place the Menu example menu block into a layout region. There are a number of examples there, from the most basic (like this one) to extravagant mappings of loaded placeholder arguments. Enjoy!');
- return '<div>' . $base_content . '</div><br /><div>' . $content . '</div>';
- }
-
- * Page callback for use with most of the menu entries.
- *
- * The arguments it receives determine what it outputs.
- *
- * @param string $content
- * The base content to output.
- * @param string $arg1
- * First additional argument from the path used to access the menu
- * @param string $arg2
- * Second additional argument.
- */
- function _menu_example_menu_page($content = NULL, $arg1 = NULL, $arg2 = NULL) {
- $output = '<div>' . $content . '</div>';
-
- if (!empty($arg1)) {
- $output .= '<div>' . t('Argument 1=%arg', array('%arg' => $arg1)) . '</div>';
- }
- if (!empty($arg2)) {
- $output .= '<div>' . t('Argument 2=%arg', array('%arg' => $arg2)) . '</div>';
- }
- return $output;
- }
-
- * Implements hook_permission().
- *
- * Provides a demonstration access string.
- */
- function menu_example_permission() {
- return array(
- 'access protected menu example' => array(
- 'title' => t('Access the protected menu example'),
- ),
- );
-
- }
-
- * Determine whether the current user has the role specified.
- *
- * @param string $role_name
- * The role required for access
- *
- * @return bool
- * True if the acting user has the role specified.
- */
- function menu_example_custom_access($role_name) {
- $access_granted = in_array($role_name, $GLOBALS['user']->roles);
- return $access_granted;
- }
-
- * Utility function to provide mappings from integers to some strings.
- *
- * This would normally be some database lookup to get an object or array from
- * a key.
- *
- * @param int $id
- * The integer key.
- *
- * @return string
- * The string to which the integer key mapped, or NULL if it did not map.
- */
- function _menu_example_mappings($id) {
- $mapped_value = NULL;
- static $mappings = array(
- 1 => 'one',
- 2 => 'two',
- 3 => 'three',
- 99 => 'jackpot! default',
- );
- if (isset($mappings[$id])) {
- $mapped_value = $mappings[$id];
- }
- return $mapped_value;
- }
-
- * The special _load function to load menu_example.
- *
- * Given an integer $id, load the string that should be associated with it.
- * Normally this load function would return an array or object with more
- * information.
- *
- * @param int $id
- * The integer to load.
- *
- * @return string
- * A string loaded from the integer.
- */
- function menu_example_id_load($id) {
-
-
- $mapped_value = _menu_example_mappings($id);
- if (!empty($mapped_value)) {
- return t('Loaded value was %loaded', array('%loaded' => $mapped_value));
- }
- else {
- return t('Sorry, the id %id was not found to be loaded', array('%id' => $id));
- }
- }
-
- * Implements hook_menu_alter().
- *
- * Changes the path 'examples/menu_example/menu_original_path' to
- * 'examples/menu_example/menu_altered_path'.
- * Changes the title callback of the 'user/UID' menu item.
- *
- * Change the path 'examples/menu_example/menu_original_path' to
- * 'examples/menu_example/menu_altered_path'. This change will prevent the
- * page from appearing at the original path (since the item is being unset).
- * You will need to go to examples/menu_example/menu_altered_path manually to
- * see the page.
- *
- * Remember that hook_menu_alter() only runs at menu_rebuild() time, not every
- * time the page is built, so this typically happens only at cache clear time.
- *
- * The $items argument is the complete list of menu router items ready to be
- * written to the menu_router table.
- */
- function menu_example_menu_alter(&$items) {
- if (!empty($items['examples/menu_example/menu_original_path'])) {
- $items['examples/menu_example/menu_altered_path'] = $items['examples/menu_example/menu_original_path'];
- $items['examples/menu_example/menu_altered_path']['title'] = 'Menu item altered by hook_menu_alter()';
- unset($items['examples/menu_example/menu_original_path']);
- }
-
-
-
- if (!empty($items['user/%user'])) {
- $items['user/%user']['title callback'] = 'menu_example_user_page_title';
- }
- }
-
- * Title callback to rewrite the '/user' menu link.
- *
- * @param string $base_string
- * string to be prepended to current user's name.
- */
- function _menu_example_simple_title_callback($base_string) {
- global $user;
- $username = !empty($user->name) ? $user->name : t('anonymous');
- return $base_string . ' ' . $username;
- }
-
- * Title callback to rename the title dynamically, based on user_page_title().
- *
- * @param object $account
- * User account related to the visited page.
- */
- function menu_example_user_page_title($account) {
- return is_object($account) ? t("@name's account", array('@name' => user_format_name($account))) : '';
- }
-
- * Implements hook_menu_link_alter().
- *
- * This code will get the chance to alter a menu link when it is being saved
- * in the menu interface at admin/build/menu. Whatever we do here overrides
- * anything the user/administrator might have been trying to do.
- */
- function menu_example_menu_link_alter(&$item, $menu) {
-
-
- if ($item['link_path'] == 'devel/cache/clear') {
- $item['link_title'] = 'Clear Cache';
- };
- }
-
- * Loads an item based on its $id.
- *
- * In this case we're just creating a more extensive string. In a real example
- * we would load or create some type of object.
- *
- * @param int $id
- * Id of the item.
- */
- function menu_example_arg_optional_load($id) {
- $mapped_value = _menu_example_mappings($id);
- if (!empty($mapped_value)) {
- return t('Loaded value was %loaded', array('%loaded' => $mapped_value));
- }
- else {
- return t('Sorry, the id %id was not found to be loaded', array('%id' => $id));
- }
- }
-
- * Utility function to provide default argument for wildcard.
- *
- * A to_arg() function is used to provide a default for the arg in the
- * wildcard. The purpose is to provide a menu link that will function if no
- * argument is given. For example, in the case of the menu item
- * 'examples/menu_example/default_arg/%menu_example_arg_optional' the third argument
- * is required, and the menu system cannot make a menu link using this path
- * since it contains a placeholder. However, when the to_arg() function is
- * provided, the menu system will create a menu link pointing to the path
- * which would be created with the to_arg() function filling in the
- * %menu_example_arg_optional.
- *
- * @param string $arg
- * The arg (URL fragment) to be tested.
- */
- function menu_example_arg_optional_to_arg($arg) {
-
- return (empty($arg) || $arg == '%') ? 99 : $arg;
- }
- * @} End of "defgroup menu_example".
- */