Conditional fieldgroups/fieldsets in Drupal 7

Posted by seane on Stack Overflow See other posts from Stack Overflow or by seane
Published on 2012-06-14T23:02:45Z Indexed on 2012/07/06 3:16 UTC
Read the original article Hit count: 118

Filed under:
|
|

Background: In Drupal 7, I have created a form with CCK (aka the Field UI). I used the Field group module to create a fieldgroup, but I need it to be conditional, meaning it will only display depending on a previous answer.

Previous research: To create a conditional field, you can use hook_form_alter() to edit the #states attribute like so:

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

However, there seems to be no way to use the States API for fieldgroups. One thing to note is that, while fields are stored in $form, fieldgroups are stored in $form['#groups'] as well as in $form['#fieldgroups']. I don't know how to distinguish between these, and with this in mind, I have tried to apply a #states attribute to a fieldgroup in the same manner as above. However, it only produces server errors.

Question: Is there a way to make a fieldgroup display conditionally using the States API or some alternative approach?

© Stack Overflow or respective owner

Related posts about php

Related posts about drupal