我有一個模塊,可以構建包含字段集的表單。而不是使用<legend>
元素來呈現字段集標題,而是希望將此內容置於<div>
元素中。但是我只想改變我的模塊返回的表單的行爲,所以我不想將任何新的功能放到我的主題的template.php文件中。如何在drupal模塊的字段集中使用自定義#theme函數?
在mymod.module我已經定義:
// custom rendering function for fieldset elements
function theme_mymod_fieldset($element) {
return 'test';
}
// implement hook_theme
function mymod_theme() {
return array(
'mymod_fieldset' => array('arguments' => array('element' => NULL)),
'mymod_form' => array('arguments' => array())
);
}
// return a form that is based on the 'Basic Account Info' category of the user profile
function mymod_form() {
// load the user's profile
global $user;
$account = user_load($user->uid);
// load the profile form, and then edit it
$form_state = array();
$form = drupal_retrieve_form('user_profile_form', $form_state, $account, 'Basic Account Info');
// set the custom #theme function for this fieldset
$form['Basic Account Info']['#theme'] = 'mymod_fieldset';
// more form manipulations
// ...
return $form;
}
當我的頁面獲取呈現,我期望看到代表「基本帳戶信息」通過我的測試消息「測試」被整體替換字段集。相反,會發生什麼情況是,<fieldset>
和<legend>
元素呈現爲正常,但與字段集的體由測試消息,而不是代替,就像這樣:
<fieldset>
<legend>Basic Account Info</legend>
test
</fieldset>
爲什麼沒有我的#theme功能有機會替換整個<fieldset>
元素?如果我用這個函數包裝一個文本字段,我可以完全替代<input>
元素及其標籤。此外,如果我在我的網站的template.php中爲theme_fieldset提供覆蓋,它會按預期工作,我可以完全替換<fieldset>
,所以我知道這是可能的。
將#主題功能提供給模塊內的現場組有什麼不同?
我有這個確切的問題。絕對是表單API中的某種錯誤..太刺激了..你有沒有找到解決方法? – tmsimont 2011-09-21 14:23:37