2013-02-14 50 views
0

我試圖建立由分層複選框類似如下的配置頁面: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

回答

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. 
    ), 
), 
); 
} 
+0

非常感謝您的支持。只需要一點幫助 - 可以修改它以動態地循環訪問網站上的一系列節點類型? – sisko 2013-02-15 13:41:13

+0

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