You are here

创建一个菜单项

g089h515r806 的头像
Submitted by g089h515r806 on 星期六, 2009-08-01 15:26

老葛的Drupal培训班 Think in Drupal

通过在你的模块中使用菜单钩子来钩住这一流程。这样你就可以定义包含在menu_router表中的菜单项。让我们构建一个名为menufun.module的示例模块,通过它来学习菜单系统。我们将Drupal路径menufun映射到PHP函数menufun _hello()上。首先,我们需要一个名为menufun.info的文件,位于sites/all/modules/custom/menufun/menufun.info:

 
; $Id$
name = Menu Fun
description = Learning about the menu system.
package = Pro Drupal Development
core = 6.x
 
    接着我们创建sites/all/modules/custom/menufun/menufun.module文件,它包含了我们的hook_menu()实现,以及我们想要运行的函数:
<?php
// $Id$
/**
* @file
* Use this module to learn about Drupal's menu system.
*/
/**
* Implementation of hook_menu().
*/
function menufun_menu() {
    $items['menufun'] = array(
        'page callback' => 'menufun_hello',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );
 
    return $items;
}
/**
* Page callback.
*/
function menufun_hello() {
    return t('Hello!');
}
 
Enabling the module at Administer ä Site building äModules causes the menu item to
be inserted into the router table, so Drupal will now find and run our function when we go to http://example.com/?q=menufun, as shown in Figure 4-3.
 
    在“管理➤站点构建 ➤模块”中,启用该模块,这样就会将该菜单项插入到menu_router表中,这样,当我们访问http://example.com/?q=menufun时,Drupal就可以找到菜单项并运行我们的函数了,如图4-3所示。
 
    这里需要注意的要点是,我们定义了一个路径,并将其映射到了一个函数上。该路径是一个Drupal路径。我们使用该路径作为$items数组的键。你还会注意到这个路径的名字和模块的名字是一样的。这里主要是用来保证有一个干净的URL命名空间。实际上,你可以在这里定义各种路径。
   
4-3.菜单项使得Drupal能够找到和运行menufun_hello()函数。
 

Drupal版本: