放牧代码和思想
专注自然语言处理、机器学习算法
    This thing called love. Know I would've. Thrown it all away. Wouldn't hesitate.

Chapter 04: The Menu System代码详解

Drupal7专业开发指南 第三版,菜单系统的代码,测试通过

menufun_greeting.inc

<?php
 
function menufun_greeting ($first_name = '', $last_name = '', $from_first_name = '', 
        $from_last_name = '')
{
    return t(
            'Hello @first_name @last_name from @from_first_name @from_last_name', 
            array(
                    '@first_name' => $first_name,
                    '@last_name' => $last_name,
                    '@from_first_name' => $from_first_name,
                    '@from_last_name' => $from_last_name
            ));
}
 
function menufun_farewell ()
{
    return t('Goodbye');
}

menufun.info

name = Menu Fun
description = Learning about the menu system.
package = Pro Drupal Development
core = 7.x
files[] = menufun.module
<?php
 
/**
 * @file
 * Use this module to learn about Drupal's menu system.
 * 使用这个模块来学习菜单系统
 */
 
/**
 * Implementation of hook_menu()
 * hook menu
 */
// function menufun_menu ()
// {
// $items['menufun'] = array(
// 'title' => 'Greeting', // 一个必须字段,是未翻译的title
// 'page callback' => 'menufun_hello', // 当用户浏览此路径时调用的函数,用来显示web页面
// 'access callback' => TRUE, //
// 一个函数,返回一个布尔值,判定用户是否有访问该菜单项的权限;默认为user_access(),除非从父菜单项继承一个值
// 'type' => MENU_CALLBACK // 一个标志,描述菜单项的特性,MENU_CALLBACK
// 回调简单地注册一个路径,URL访问时恰当的函数将被触发
// );
// return $items;
// }
// function menufun_menu ()
// {
// $items['menufun'] = array(
// 'title' => 'Greeting',
// 'page callback' => 'menufun_hello',
// 'page arguments' => array('Jane', 'Doe'), // 要传递给页面回调函数的参数的数组
// 'access callback' => TRUE,
// 'type' => MENU_CALLBACK
// );
 
// return $items;
// }
// function menufun_menu ()
// {
// $items['menufun'] = array(
// 'title' => 'Menu Fun',
// 'page callback' => 'menufun_greeting',
// 'file' => 'menufun_greeting.inc',
// 'page arguments' => array(
// 'Jane',
// 'Doe'
// ),
// 'access callback' => TRUE,
// 'type' => MENU_CALLBACK
// );
// return $items;
// }
// function menufun_menu ()
// {
// $items['menufun'] = array(
// 'title' => 'Greeting',
// 'page callback' => 'menufun_hello',
// 'page arguments' => array(
// 'Jane',
// 'Doe'
// ),
// 'access callback' => TRUE,
// 'weight' => - 1
// );
// return $items;
// }
// 多个菜单
// function menufun_menu ()
// {
// $items['menufun'] = array(
// 'title' => 'Menu Fun',
// 'page callback' => 'menufun_greeting',
// 'file' => 'menufun_greeting.inc',
// 'page arguments' => array(
// 'Jane',
// 'Doe'
// ),
// 'access callback' => TRUE,
// 'type' => MENU_NORMAL_ITEM,
// 'weight' => '-1'
// );
// $items['menufun/farewell'] = array( // 作为Menu Fun的子菜单
// 'title' => 'Farewell',
// 'page callback' => 'menufun_farewell',
// 'file' => 'menufun_greeting.inc',
// 'access callback' => TRUE,
// 'type' => MENU_NORMAL_ITEM
// );
// return $items;
// }
 
// 带有权限检验
/**
 * Implementation of hook_menu().
 */
// function menufun_menu ()
// {
//     $items['menufun'] = array(
//             'title' => 'Menu Fun',
//             'page callback' => 'menufun_greeting',
//             'file' => 'menufun_greeting.inc',
//             'page arguments' => array(
//                     'Jane',
//                     'Doe'
//             ),
//             'access callback' => 'user_access',     //  user_access()函数是默认的访问回调,如果你不定义访问回调函数,你的访问参数将有菜单系统传递给user_access()函数。
//             'access arguments' => array(            //  传递给访问回调函数的参数数组
//                     'receive greeting'              //  定义一个叫做receive greeting的权限
//             ),
//             'type' => MENU_NORMAL_ITEM,
//             'weight' => '-1'
//     );
//     $items['menufun/farewell'] = array(
//             'title' => 'Farewell',
//             'page callback' => 'menufun_farewell',
//             'file' => 'menufun_greeting.inc',
//             'access callback' => 'user_access',
//             'access arguments' => array(
//                     'receive greeting'
//             ),
//             'type' => MENU_NORMAL_ITEM
//     );
//     return $items;
// }
// drupal 本地化测试
function menufun_menu ()
{
    $items['menufun'] = array(
            'title' => 'Greeting',
            'title callback' => 'menufun_title',
            'description' => 'A salutation.',
            'page callback' => 'menufun_hello',
            'access callback' => TRUE
    );
    return $items;
}
 
/**
 * 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
//     ));
// }
function menufun_title ()
{
    // 使得页面标题和菜单标题不同
    drupal_set_title(t('The page title'));
    $now = format_date(time());
    return t('It is now @time', array(
            '@time' => $now
    ));
}
 
// /**
//  * Page callback
//  * 简单的回调函数,仅仅在页面上显示标题和欢迎
//  */
// function menufun_hello ()
// {
//     return t('Hello!');
// }
 
// function menufun_hello ($first_name = '', $last_name = '')
// {
//     return t('Hello @first_name @last_name',        //@first_name是一个占位符,t函数有t($string, $args = array(), $langcode = NULL)的类型 
//             array(
//                     '@first_name' => $first_name,
//                     '@last_name' => $last_name
//             ));
// }
// function menufun_hello ($first_name = '', $last_name = '')
// {
//     return t(
//             'Hello @first_name @last_name from @from_first_name @from_last_name', 
//             array(
//                     '@first_name' => $first_name,
//                     '@last_name' => $last_name
//             ));
// }
 
/**
 * Implementation of hook_permission()
 * 权限控制,往drupal的模块——权限页面添加一个权限选项
 */
function menufun_permission ()
{
    return array(
            'receive greeting' => array(
                    'title' => t('Receive a greeting'),
                    'description' => t('Allow users receive a greeting message')
            )
    );
}
 
/**
 * Implementation of hook_menu_alter().
 *
 * @param array $items
 * Menu items keyed bu 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']);
}
 
/**
 * 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() implementation.
    $user = drupal_anonymous_user();
     
    drupal_goto('http://drupal.org/');
}

一些结果:

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » Chapter 04: The Menu System代码详解

评论 欢迎留言

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

我的作品

HanLP自然语言处理包《自然语言处理入门》