0

我正在尝试在自定义结帐窗格中添加有条件显示的字段集。与标准表单不同,无论状态如何,始终显示字段集。

例如,在下面的代码中,我希望根据“hungry”单选按钮的值显示“hungry_fields”字段集。

function hungry_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {

  $pane_form['hungry'] = array(
    '#type' => 'radios',
    '#options' => array('yes' => t('Yes'), 'no' => t('No')),
    '#required' => TRUE,
    '#title' => t('I am hungry')
  );

  $pane_form['hungry_fields'] = array(
    '#title' => 'Hungry',
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#states' => array(
      'visible' => array(
        ':input[name="hungry"]' => array('value' => 'yes'),
      ),
    ),
  );

  return $pane_form;

}

我是 Drupal Commerce 的新手,所以我完全有可能遗漏了一些东西。

4

1 回答 1

1

我通过使用带有 drupal_get_form() 的表单回调而不是直接将字段添加到数组来解决此问题。

例如:

function hungry_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {

  $pane_form['form'] = drupal_get_form('hungry_form');

}

function hungry_form($form, &$form_state) {
  ...
}

使用这种方法,表格可以正常工作。

于 2014-02-28T04:46:17.963 回答