You are here

通配符和to_arg()函数的特殊情况

g089h515r806 的头像
Submitted by g089h515r806 on 星期一, 2009-08-03 15:00

老葛的Drupal培训班 Think in Drupal

Drupal在为一个菜单项构建链接时,它要查找的to_arg()函数,是基于Drupal路径中通配符后面的字符串生成的。它可以是任意的字符串,例如:
/**
* Implementation of hook_menu().
*/
function_menufun_menu() {
    $items['menufun/%a_zoo_animal'] = array(
        'title' => 'Hi',
        'page callback' => 'menufun_hello',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
        'weight' => -10
    );
    return $items;
}
 
function a_zoo_animal_to_arg($arg) {
    // $arg is '%' since it is a wildcard
    // Let's replace it with a zoo animal.
    return 'tiger';
}
 
    这样,链接“Hi”就会出现在导航区块中了。该链接的URL为http://example.com/?q=menufun/tiger。通常,你不会像这个简单的例子中这样,使用一个静态字符串来替换通配符,而是使用动态的东西,比如当前用户的uid或者当前节点的nid。

Drupal版本: