Drupal表单API复选框架层次结构

时间:2013-02-14 15:55:16

标签: forms api drupal-7 hierarchy checkbox

我正在尝试设置一个包含分层复选框的配置页面,如下所示:

-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

1 个答案:

答案 0 :(得分:0)

您可以使用新的表单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 =&gt;在字段集中为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.
    ),
  ),
);
}