Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

Download from Wow! eBook <www.wowebook.com>

CHAPTER 4 THE MENU SYSTEM

'access callback' => TRUE, 'type' => MENU_NORMAL_ITEM, 'weight' => -10

);

return $items;

}

function menufun_hello($animal) { return t(“Hello $animal”);

}

function a_zoo_animal_to_arg($arg) {

//$arg is '%' since it is a wildcard

//Let's replace it with a zoo animal. return 'tiger';

}

This causes the link “Hi” to appear in the navigation block. The URL for the link is http://example.com/?q=menufun/tiger. Normally, you would not replace the wildcard with a static string as in this simple example. Rather, the to_arg() function would produce something dynamic, like the uid of the current user or the nid of the current node.

Altering Menu Items from Other Modules

When Drupal rebuilds the menu_router table and updates the menu_link tables (for example, when a new module is enabled), modules are given a chance to change any menu item by implementing hook_menu_alter(). For example, the “Log off” menu item logs out the current user by calling user_logout(), which destroys the user’s session and then redirects the user to the site’s home page. The user_logout() function lives in modules/user/user.pages.inc, so the menu item for the Drupal path has a file key defined. So normally Drupal loads the file modules/user/user.pages.inc and runs the user_logout() page callback when a user clicks the “Log out” link from the navigation block. Let’s change that to redirect users who are logging out to drupal.org.

/**

*Implementation of hook_menu_alter().

*@param array $items

*Menu items keyed by path.

*/

function menufun_menu_alter(&$items) {

//Replace the page callback to 'user_logout' with a call to

//our own page callback.

$items['logout']['page callback'] = 'menufun_user_logout'; $items[‘logout’][‘access callback’] = ‘user_is_logged_in’;

//Drupal no longer has to load the user.pages.inc file

//since it will be calling our menufun_user_logout(), which

//is in our module -- and that's already in scope.

unset($items['logout']['file']);

}

80

CHAPTER 4 THE MENU SYSTEM

/**

*Menu callback; logs the current user out, and redirects to drupal.org.

*This is a modified version of user_logout().

*/

function menufun_user_logout() { global $user;

watchdog('menufun', 'Session closed for %name.', array('%name' => $user->name));

//Destroy the current session: session_destroy();

//Run the 'logout' operation of the user hook so modules can respond

//to the logout if they want to.

module_invoke_all('user', 'logout', NULL, $user);

//Load the anonymous user so the global $user object will be correct

//on any hook_exit() implementations.

$user = drupal_anonymous_user();

drupal_goto('http://drupal.org/');

}

Before our hook_menu_alter() implementation ran, the menu item for the logout path looked like this:

array(

 

'access callback'

=> 'user_is_logged_in',

'file'

=> 'user.pages.inc',

'module'

=> 'user',

'page callback'

=> 'user_logout',

'title'

=> 'Log out',

'weight'

=> 10,

)

 

And after we have altered it, the page callback is now set to menufun_user_logout:

array(

 

'access callback'

=> 'user_is_logged_in',

'module'

=> 'user',

'page callback'

=> 'menufun_user_logout',

'title'

=> 'Log out',

'weight'

=> 10,

)

 

81

CHAPTER 4 THE MENU SYSTEM

Altering Menu Links from Other Modules

When Drupal saves a menu item to the menu_link table, modules are given a chance to change the link by implementing hook_menu_link_alter(). Here is how the “Log out” menu item could be changed to be titled “Sign off.”

/**

*Implements hook_menu_link_alter().

*@param $item

*Associative array defining a menu link as passed into menu_link_save()

*/

function menufun_menu_link_alter(&$item) { if ($item['link_path'] == 'user/logout') {

$item['link_title'] = 'Sign off';

}

}

This hook should be used to modify the title or weight of a link. If you need to modify other properties of a menu item, such as the access callback, use hook_menu_alter() instead.

Note The changes made to a menu item in hook_menu_link_alter() are not overrideable by the user interface that menu.module presents at Administer -> Site building -> Menus.

Kinds of Menu Items

When you are adding a menu item in the menu hook, one of the possible keys you can use is the type. If you do not define a type, the default type MENU_NORMAL_ITEM will be used. Drupal will treat your menu item differently according to the type you assign. Each menu item type is composed of a series of flags, or attributes (see includes/menu.inc). Table 4-3 lists the menu item type flags.

82

CHAPTER 4 THE MENU SYSTEM

Table 4-3. Menu Item Type Flags

 

 

 

 

 

 

 

 

Binary

Hexadecimal

Decimal

Constant

Description

 

 

 

 

 

 

000000000001

0x00

01

1

MENU_IS_ROOT

Menu item is the root of

 

 

 

 

 

the menu tree

000000000010

0x00

02

2

MENU_VISIBLE_IN_TREE

Menu item is visible in the

 

 

 

 

 

menu tree

000000000100

0x00

04

4MENU_VISIBLE_IN_BREADCRUMB

Menu item is visible in the

 

 

 

 

 

breadcrumb

000000001000

0x00

08

8MENU_LINKS_TO_PARENT

Menu item links back to

 

 

 

 

 

its parent

000000100000

0x00

20

32

MENU_MODIFIED_BY_ADMIN

Menu item can be

 

 

 

 

 

modified by administrator

000001000000

0x00

40

64

MENU_CREATED_BY_ADMIN

Menu item was created by

 

 

 

 

 

administrator

000010000000

0x00

80

128

MENU_IS_LOCAL_TASK

Menu item is a local task

000100000000

0x01

00

256

MENU_IS_LOCAL_ACTION

Menu item is a local

 

 

 

 

 

action

 

 

 

 

 

 

For example, the constant MENU_NORMAL_ITEM (define('MENU_NORMAL_ITEM', MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IN_BREADCRUMB) has the flags MENU_VISIBLE_IN_TREE and MENU_VISIBLE_IN_BREADCRUMB, as shown in Table 4-4.

Table 4-4. Flags of the Menu Item Type MENU_NORMAL_ITEM

Binary

Constant

 

 

000000000010 MENU_VISIBLE_IN_TREE

000000000100 MENU_VISIBLE_IN_BREADCRUMB

000000000110 MENU_NORMAL_ITEM

Therefore, MENU_NORMAL_ITEM has the following flags: 000000000110. Table 4-5 shows the available menu item types and the flags they express.

83

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]