【问题标题】:Conditional fieldgroups/fieldsets in Drupal 7Drupal 7 中的条件字段组/字段集
【发布时间】:2012-06-14 23:02:45
【问题描述】:

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

以往研究:要创建条件字段,可以使用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'),
      ),
    );
  }
}

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

问题:有没有办法使用 States API 或其他方法有条件地显示字段组?

【问题讨论】:

    标签: php drupal drupal-7


    【解决方案1】:

    你必须使用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),
        ),
      );
    }
    

    这非常有效。如果 A 组在另一个组中,请执行此操作

    $element['groupA']['groupB']['#states'] etc....
    

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

    $element['your_field_group']['#attributes']['id'] = 'some-id';
    $element['yout_field_group']['#id'] = 'some-id';
    

    【讨论】:

    • 哇,这比我的解决方案优雅多了!谢谢!
    • 这种方法对我有用,但我遇到了一个大问题。创建不同内容类型的节点时,此代码仍将执行。当我试图在代码中添加一个条件来检查特定的节点类型时,似乎 $element 将值从另一种形式传递到了这种形式。主要问题:节点类型不正确。还有人经历过这个吗?我在 D7。
    • @MTsrb 尝试$element['#form_id']上的条件
    【解决方案2】:

    这是我想出的最简单的解决方案。这基本上有 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;
    }
    

    (2.) 然后我使用Field group 模块来改变内容的显示。我通过转到我的内容类型并使用“管理显示”选项卡创建一个字段组并将我的字段添加到其中来做到这一点。这样一来,表单和保存的内容上的字段就会显示为属于同一组。

    【讨论】:

      【解决方案3】:

      也许你可以试试看this module的代码,帮助你找到一个想法。

      【讨论】:

      • 感谢您的提示。我实际上并没有最终看它,因为我终于有了一些工作。如果您有兴趣,请查看发布的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多