Drupal的形式API复选框层次

问题描述:

我试图建立由分层复选框类似如下的配置页面:Drupal的形式API复选框层次

-Parent#1 
    --option#1 
    --option#2 

-Parent#2 
    --option#1 
    --option#2 

为了实现上述的布局,我使用下面的代码:

$form['categories']['templates']['parent1'] = array(
    '#type'   => 'checkboxes', 
    '#title'  => t('Select the appropriate actions for the form based on the indicated template'), 
    '#options'  => array("Parent#1"), 
    '#multiple'  => TRUE, 
); 
$form['categories']['templates']['parent1']['actions'] = array(
    '#type'   => 'checkboxes', 
    '#options'  => array("P1 - option#1", "P1 - option#2"), 
    '#multiple'  => TRUE, 
); 

$form['categories']['templates']['parent2'] = array(
    '#type'   => 'checkboxes', 
    '#title'  => t('Select the appropriate actions for the form based on the indicated template'), 
    '#options'  => array("Parent#2"), 
    '#multiple'  => TRUE, 
); 
$form['categories']['templates']['parent2']['actions'] = array(
    '#type'   => 'checkboxes', 
    '#options'  => array("P2 - option#1", "P2 - option#2"), 
    '#multiple'  => TRUE, 
); 

但是我得到的效果并不理想。我连着什么代码生成的图像:

enter image description here

+0

你的意思是增加点击时检查其所有的孩子家长chekcbox? – 2013-02-15 00:43:02

+0

就是这样的。儿童复选框的目的是在父母被选中时变得可见 – sisko 2013-02-15 09:14:05

您可以使用新的API表格功能,#states实现这一目标。

请注意,为了便于使用,我使用fieldset包装了条件复选框。

<?php 
    $form['categories']['templates']['parent1'] = array(
    '#type'   => 'checkboxes', 
    '#title'  => t('Select the appropriate actions for the form based on the indicated template'), 
    '#options'  => array(1 => 'Parent#1'), // Note this! You can't use 0 as a key in #options. 
    '#multiple'  => TRUE, 
); 
    $form['categories']['templates']['parent1']['wrapper'] = array(
    '#type' => 'fieldset', 
    '#title' => t('Options for @option', array('@option' => 'Parent#1')), 
    '#states' => array(
     'visible' => array(
     ':input[name="parent1[1]"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field. 
    ), 
    ), 
); 
    $form['categories']['templates']['parent1']['wrapper']['actions'] = array(
    '#type'   => 'checkboxes', 
    '#options'  => array("P1 - option#1", "P1 - option#2"), // This is also wrong. You must define a key => value pair for #options or somehow avoid using 0 as the key. 
    '#multiple'  => TRUE, 
); 
?> 

您可能需要使用#tree => TRUE的字段集,以避免从Drupal的合并同一键的值加在一起。

此外,复选框字段类型中不需要#multiple

更新

实例与节点类型:

<?php 
$node_types = node_type_get_names(); 
$form['categories']['templates']['parent1'] = array(
    '#type'   => 'checkboxes', 
    '#title'  => t('Select the appropriate actions for the form based on the indicated template'), 
    '#options'  => $node_types, 
); 
foreach ($node_types as $type => $label) { 
    $form['categories']['templates']['node-options'.$type] = array(
    '#type'   => 'checkboxes', 
    '#title'  => t('Options for @type', array('@type' => $label)), 
    '#options'  => array("Parent#1"), 
    '#multiple'  => TRUE, 
    '#states' => array(
    'visible' => array(
     ':input[name="parent1['.$type.']"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field. 
    ), 
), 
); 
} 
+0

非常感谢您的支持。只需要一点帮助 - 可以修改它以动态地循环访问网站上的一系列节点类型? – sisko 2013-02-15 13:41:13

+0

非常感谢。感谢您的帮助 – sisko 2013-02-15 14:55:30