2011-12-22 75 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