Drupal 7中的条件字段组/字段集

sea*_*ane 10 php drupal drupal-7

背景:在Drupal 7中,我用CCK(又名Field UI)创建了一个表单.我使用Field组模块来创建一个fieldgroup,但我需要它是有条件的,这意味着它只会根据之前的答案显示.

以前的研究:要创建条件字段,可以使用hook_form_alter()来编辑#states属性,如下所示:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'person_info_node_form') {
    // Display 'field_maiden_name' only if married
    $form['field_maiden_name']['#states'] = array(
      'visible' => array(
        ':input[name="field_married[und]"]' => array('value' => 'Yes'),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,似乎没有办法将States API用于fieldgroups.有一点要注意的是,虽然字段存储在$form,FIELDGROUPS存储在$form['#groups']和中$form['#fieldgroups'].我不知道如何区分这些,并且考虑到这一点,我试图以与上面相同的方式将#states属性应用于字段组.但是,它只会产生服务器错误.

问题:有没有办法使用States API或某种替代方法有条件地显示字段组?

小智 6

您必须使用hook_field_group_build_pre_render_alter()

简单地:

function your_module_field_group_build_pre_render_alter(&$element) {
  $element['your_field_group']['#states'] = array(
    'visible' => array(
      ':input[name="field_checkbox"]' => array('checked' => TRUE),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)

这是完美的。如果组A在另一个组中,请执行此操作

$element['groupA']['groupB']['#states'] etc....
Run Code Online (Sandbox Code Playgroud)

如果不存在,则可能需要添加id属性:

$element['your_field_group']['#attributes']['id'] = 'some-id';
$element['yout_field_group']['#id'] = 'some-id';
Run Code Online (Sandbox Code Playgroud)


sea*_*ane 2

这是我想出的最简单的解决方案。本质上有两个部分:(1.) 以编程方式更改表单的显示,以及 (2.) 使用 GUI 更改内容的显示。

(1.) 首先,我使用hook_form_alter()以编程方式创建条件字段集并向其中添加现有字段。代码如下所示。

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'FORM_ID_node_form') {
    // programmatically create a conditional fieldset
    $form['MYFIELDSET'] = array( // do NOT name the same as a 'Field group' fieldset or problems will occur
      '#type' => 'fieldset',
      '#title' => t('Conditional fieldset'),
      '#weight' => intval($form['field_PARENT']['#weight'])+1, // put this fieldset right after it's "parent" field
      '#states' => array(
        'visible' => array(
          ':input[name="field_PARENT[und]"]' => array('value' => 'Yes'), // only show if field_PARENT == 'Yes'
        ),  
      ),  
    );

    // add existing fields (created with the Field UI) to the
    // conditional fieldset
    $fields = array('field_MYFIELD1', 'field_MYFIELD2', 'field_MYFIELD3');
    $form = MYMODULE_addToFieldset($form, 'MYFIELDSET', $fields);
  }
}

/**
 * Adds existing fields to the specified fieldset.
 *
 * @param  array   $form Nested array of form elements that comprise the form.
 * @param  string  $fieldset The machine name of the fieldset.
 * @param  array   $fields An array of the machine names of all fields to
 *                   be included in the fieldset.
 * @return array   $form The updated form.
 */
function MYMODULE_addToFieldSet($form, $fieldset, $fields) {
  foreach($fields as $field) {
    $form[$fieldset][$field] = $form[$field]; // copy existing field into fieldset
    unset($form[$field]); // destroy the original field or duplication will occur
  }

  return $form;
}
Run Code Online (Sandbox Code Playgroud)

(2.) 然后我使用字段组模块来更改内容的显示。我通过转到我的内容类型并使用“管理显示”选项卡创建一个字段组并将我的字段添加到其中来完成此操作。这样,这些字段在表单和保存的内容上都将显示为属于同一组。