Revision of 有關 hook_menu title arguments from Mon, 2012-12-17 21:09

hook_menu中有一個選項是 title arguments
page arguments 大家可能用得比較多
而使用 title arguments 的機會比較少
而 API reference 也沒有有關的例子可以參考
我便約略介紹一下

首先,有關 title 的參數有三個:
title,字符,是未經翻譯的標題,會用於 <title> 內,一般的 theme 也會在 content 用 h1 輸出
title callback,可選參數,預設是 t(),這讓上面的 title 會預設經過翻譯然後輸出,所以上面的 title 不需要使用 t() 包起來
title argument,可選參數,必需是陣列,例如array(1)

假設 hook_menu 定義:

<?php
items
['admin/settings/joe/abc'] = array(
 
'title' => 'Testing page',
);
?>

最後 title 的處理會是: <?php t('Testing page') ?>

或者:

<?php
items
['admin/settings/joe/abc'] = array(
 
'title' => 'Testing page',
 
'title callback' => 'strtoupper'
);
?>

最後 title 的處理會是: <?php strtoupper('Testing page') ?>
以上的例子會令 title 避開 t()
所以應該自定義一個 callback:
<?php
items
['admin/settings/joe/abc'] = array(
 
'title' => 'Testing page',
 
'title callback' => 'strtoupper_t'
);

function
strtoupper_t($str) {
  return
t(strtoupper($str));
}
?>
Google