0

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

-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,
);

但我得到的效果并不如预期。我附上了代码生成的图像:

在此处输入图像描述

4

1 回答 1

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=> 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.
    ),
  ),
);
}
于 2013-02-15T12:17:38.653 回答