2011-12-22 52 views
1

多個複選框,我想在我的D7-表單中添加一些複選框。出於某種原因,下面的代碼片段不起作用。任何想法爲什麼或任何建議如何正確地做到這一點?添加在Drupal的形式

$options = array('A', 'B', 'C'); 
foreach ($themas as $thema) { 

     // Initialize array 
     $ra = array(); 

     // Fill up the array with different keys 
     $key = $prefix.'_thema_'.$thema->tid.'_fiche'; 
     $ra[$key]['#type'] = 'checkboxes'; 
     $ra[$key]['#name'] = $prefix.'_thema_'.$thema->tid.'_opties'; 
     $ra[$key]['#options'] = $options; 
} 

回答

3

我想這是因爲你在循環的每一步重新初始化$ra所以它只會包含一個複選框集。嘗試初始化它的循環之外:

$options = array('A', 'B', 'C'); 

// Initialize array 
$ra = array(); 

foreach ($themas as $thema) { 
    // Fill up the array with different keys 
    $key = $prefix.'_thema_'.$thema->tid.'_fiche'; 
    $ra[$key]['#type'] = 'checkboxes'; 
    $ra[$key]['#name'] = $prefix.'_thema_'.$thema->tid.'_opties'; 
    $ra[$key]['#options'] = $options; 
} 

$form['some_key'] = $ra; 

另外,還要確保您的$prefix字符串不以#符號開始或Drupal將認爲這是一個屬性,而不是一個需要渲染的元素。

+0

這的確是一個問題,初始化!每次都會覆蓋'#options'的值。謝謝 – Michiel 2011-12-22 15:18:31