2010-05-04 37 views
2

我有一個模塊,可以構建包含字段集的表單。而不是使用<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>,所以我知道這是可能的。

將#主題功能提供給模塊內的現場組有什麼不同?

+0

我有這個確切的問題。絕對是表單API中的某種錯誤..太刺激了..你有沒有找到解決方法? – tmsimont 2011-09-21 14:23:37

回答

0

這只是我的頭頂,但可能不同的是,因爲一個fieldset不是一個表單元素,但只是一個分隔符或石斑魚,如果你願意。也許#theme回調只適用於表單元素?

+0

回調被調用,但它似乎只能渲染字段集的內容,而不是字段集元素本身。所以基本上,如果我將theme_fieldset的原始定義複製並粘貼到我自定義的#theme函數中,我會在字段集中嵌入一個字段集。不是我所希望的: – 2010-05-04 17:39:24

0

你的代碼的概念起作用,這意味着你可以做你想做的事情。

有些事情可以解釋爲什麼它不起作用。

  • 該字段不是$form['Basic Account Info']
  • 需要清除緩存。
  • $ form ['Basic Account Info'] ['#theme']稍後會在代碼執行中丟失/覆蓋。

嘗試看看$form,然後再進行任何調整。當我試圖複製你的代碼,我遇到了一個錯誤:需要

  • user.pages.inc文件載入
2

您是否嘗試過重寫theme_fieldset()而不是使用#theme功能?我相信你可以在你的.module文件中做這樣的事情:

function mymodule_fieldset($element) { 
    // do something; 
    return $html; 
} 

這將適用於所有字段集。您可以對要影響的字段集進行$元素檢查,然後對所有其他字段使用默認實現。

看看:http://api.drupal.org/api/function/theme_fieldset/6

1

我知道這是舊的文章 - 但我已經遇到了同樣的問題。我想出了一個醜陋的工作。這絕對是Form API中的一個錯誤。也許我的臨時修復會對某人有所幫助。

我發現(和附加)bug報告在這裏:http://drupal.org/node/225698

值得一試,試圖我哈克之前修復。

我不知道孩子們都在$form['Basic Account Info']在這個例子中,但基本上你可以做的是對字段集的兒童使用drupal_render(),然後重新創建一個字段陣列從$form['Basic Account Info']分開,主題它theme()並把它傳遞迴形式數組作爲標記..

$fieldsetElement = array(
    //$child is the key of whatever child you need in the fieldset 
    //you may have to alter this for multiple children, stringing 
    //together multiple drupal_render calls on each children 
    //(#children needs to be a string.. unless your theme can handle the array) 
    '#children'=>drupal_render($form['Basic Account Info'][$child]), 
    '#attributes'=>array(),//set real attributes 
    '#title'=>$form['Basic Account Info']['#title'] 
); 
$form['Basic Account Info'] = array(
    '#type'=>'markup',//not really needed, is default 
    '#value'=>theme('mymod_fieldset',$fieldsetElement) 
); 

超級騙子盜號,可能的原因斷開與形式的國家和潛在的驗證失敗 - 但兩者都是通過反覆試驗與形式的API可以解決的。我不會推薦這個,除非你真的想用PHP和drupal form API弄髒你的雙手,但這真的是唯一的方法,除非你的模塊中沒有可變的fieldset主題......也許試試前綴和後綴?

+0

很酷的信息@tmsimont。不幸的是,我不再在這個代碼基礎上工作(或者所有關於這個問題的drupal),所以我不能真正嘗試這個。 ! – 2011-11-15 07:35:06

0

我遇到了同樣的問題。

您需要使用#theme_wrappers,而不是#theme

'#type' => 'fieldset', 
'#theme_wrappers' => array('mymodule_fieldset'), 
相關問題