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

CHAPTER 4 THE MENU SYSTEM

Tip The user_access() function is the default access callback. If you do not define an access callback, your access arguments will be passed to user_access() by the menu system.

Child menu items do not inherit access callbacks and access arguments from their parents. The access arguments key must be defined for every menu item. The access callback key must be defined only if it differs from user_access. The exception to this is any menu item of type

MENU_DEFAULT_LOCAL_TASK, which will inherit the parent access callback and access arguments, though for clarity it is best to explicitly define these keys even for default local tasks.

Title Localization and Customization

There are two types of titles, static and dynamic. Static titles are created by assigning a value to the “title” key. Dynamic titles are created through a title callback function. Drupal automatically translates static title values for you, so there’s no need to wrap the title with t(). If you use dynamic titles, through a title callback function, you are responsible for doing the translation within your callback.

'title' => t('Greeting') // No! don't use t() in menu item titles or descriptions. title callback key:

Note Descriptions are always static, set by the value of the “description key,” and are automatically translated by Drupal.

Defining a Title Callback

Titles may be created dynamically at runtime through the use of a title callback. The following example demonstrates the use of a title callback function that sets the value of the title to the current date and time. Since I’m using a title callback, the function is responsible for performing the translation before the value is returned. To perform the translation, I’ll wrap the value returned with t().

function menufun_menu() { $items['menufun'] = array(

'title' => 'Greeting',

'title callback' => 'menufun_title',

'description' => 'A salutation.', 'page callback' => 'menufun_hello', 'access callback' => TRUE,

);

return $items;

}

72

CHAPTER 4 THE MENU SYSTEM

/**

* Page callback. */

function menufun_hello() { return t('Hello!');

}

/**

* Title callback. */

function menufun_title() { $now = format_date(time());

return t('It is now @time', array('@time' => $now));

}

As shown in Figure 4-9, setting of the menu item title at runtime can be achieved through the use of a custom title callback.

Figure 4-9. Title callback setting the title of a menu item

But what if we want to decouple the menu item title from the title of the page? Easy—we set the page title using drupal_set_title():

73

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