posts - 431,  comments - 344,  trackbacks - 0

Access Control

Usually menu access is controlled by defining permissions inside the

module using hook_perm() and testing those permissions using user_access().

function mymenu_perm() {

       return array('receive greeting', 'receive goodbye');

}

function mymenu_menu($may_cache) {

       $items = array();

       if ($may_cache) {

              // Define a static menu item.

              $items[] = array(

                     'title' => t('Greeting'),

                     'path' => 'mymenu',

                     'weight' => -10,

                     'callback' => 'mymenu_hello',

                     'callback arguments' => array(t('Hi!'), t('Ho!')),

                     'access' => user_access('receive greeting')

              );

              $items[] = array(

                     'title' => t('Farewell'),

                     'path' => 'mymenu/goodbye',

                     'callback' => 'mymenu_goodbye',

                     'access' => user_access('receive goodbye')

              );

       }

       return $items;

}

Assigning Callbacks Without Adding a Link to the Menu

Often you may want to map a URL to a function without creating a visible menu item. You

can do this by assigning the MENU_CALLBACK type to your menu item, as in this example from

node.module:

$items[] = array(

       'path' => 'rss.xml',

       'title' => t('RSS feed'),

       'callback' => 'node_feed',

       'access' => user_access('access content'),

       'type' => MENU_CALLBACK

);

Displaying Menu Items As Tabs

In Drupal’s admittedly obscure menu lingo, a callback that is displayed as a tab is known as a

local task and has the type MENU_LOCAL_TASK or MENU_DEFAULT_LOCAL_TASK. The title of a local

task should be a short verb, such as “add” or “list.” Local tasks usually act on some kind of

object, such as a node, user, or workflow.

Local tasks must have a parent item in order for the tabs to be rendered. A common practice

is to assign a callback to a root path like milkshake, and then assign local tasks to paths that

extend that path, like milkshake/prepare, milkshake/drink, and so forth. Drupal has built-in

support for two levels of tabbed local tasks.

function milkshake_menu($may_cache) {

       $items = array();

       if ($may_cache) {

              $items[] = array(

                     'path' => 'milkshake',

                     'title' => t('Milkshake flavors'),

                     'callback' => 'milkshake_overview',

                     'access' => TRUE

              );

              $items[] = array(

                     'path' => 'milkshake/list',

                     'title' => t('List flavors'),

                     'type' => MENU_DEFAULT_LOCAL_TASK, //默认选上此menu

                     'access' => TRUE,

                     'weight' => 0

              );

              $items[] = array(

                     'path' => 'milkshake/add',

                     'title' => t('Add flavors'),

                     'callback' => 'milkshake_add',

                     'type' => MENU_LOCAL_TASK,

                     'access' => TRUE,

                     'weight' => 1

              );

              $items[] = array(

                     'path' => 'milkshake/list/fruity',

                     'title' => t('Fruity flavors'),

                     'callback' => 'milkshake_list',

                     'type' => MENU_LOCAL_TASK,

                     'access' => TRUE,

              );

              $items[] = array(

                     'path' => 'milkshake/list/candy',

                     'title' => t('Candy flavors'),

                     'callback' => 'milkshake_list',

                     'type' => MENU_LOCAL_TASK,

                     'access' => TRUE,

              );

       }

       return $items;

}

function milkshake_overview() {

       $output = t('The following flavors are available...');

       // ... more code here

       return $output;

}

function milkshake_add() {

       return t('milkshake add');

}

If you want the menu item to show up in the administrative menu block, you have to make

the type a MENU_NORMAL_ITEM instead of a MENU_LOCAL_TASK. And if you want it to show up in both

places, use the following:

'type' => MENU_NORMAL_ITEM | MENU_LOCAL_TASK

Programmatically Modifying Existing Menus

1Wrapping Calls to Menu Items

/**

* Implementation of hook_menu().

*/

function mymodule_menu($may_cache) {

       $items = array();

       if (!$may_cache && module_exist('devel')) { // Make sure devel.module is enabled.

              $items[] = array(

                     'path' => 'devel/cache/clear', // Same path that devel.module uses.

                     'title' => t('Wrap cache clear'),

                     'callback' => 'mymodule_clear_cache',

                     'type' => MENU_CALLBACK,

                     'access' => user_access('access devel information') // Same as devel.module.

              );

       }

}

function mymodule_clear_cache() {

       drupal_set_message('We got called first!');

       // Wrap the devel function normally called.

       devel_cache_clear();

}

2Deleting Existing Menus

$items[] = array(

       'path' => 'node/add',

       'title' => t('This should not show up'),

       'callback' => 'drupal_not_found',

       'type' => MENU_CALLBACK

);

Adding to Existing Menus

$items[] = array(

       'path' => 'admin/user/user/eradicate',

       'title' => t('Eradicate all users'),

       'callback' => 'mymodule_eradicate_users',

       'type' => MENU_LOCAL_TASK,

       'access' => TRUE

);

posted on 2007-12-03 10:01 周锐 阅读(270) 评论(0)  编辑  收藏 所属分类: PHP

只有注册用户登录后才能发表评论。


网站导航: