9

I have a custom module, and I am trying to hide a CCK fieldgroup using "#states" when a certain item is selected in a drop-down select list.

I have tried using states from within hook_form_alter(), like so:

$form['group_mygroup']['#states']['invisible'] = array(
  ':input[name="field_trigger[' . LANGUAGE_NONE. ']"]' => array( 'value' => 'hide' ),
);

This doesn't seem to do anything.

I have looked at changing the weight of my module as per various sites like http://proofgroup.com/blog/2008/dec/modifying_cck_fieldgroups_hook_form_alter.

but still to no avail. I can hide all the form elements within the fieldgroup using states but not the field group itself. Does anyone have any idea?

1

3 Answers 3

11
/**
 * Implements hook_field_group_build_pre_render_alter.
 */
function mymodule_field_group_build_pre_render_alter(&$element) {
  if (isset($element['#form_id'])) {
    if ($element['#form_id'] == 'mynodetype_node_form') {
      $element['group_myfieldgroup']['#states'] = array(
        'visible' => array(
          ':input[name="field_myfield[und]"]' => array('checked' => TRUE),
        )
      );
      $element['group_myfieldgroup']['#id'] = 'group_myfieldgroup';
    }
  }
}

I use this hook to hide my field group. It's visible only if the checkbox field_myfield is checked.

To explore $element variable in this hook I used

die('<pre>'.print_r($element, 1).'</pre>');

instead of dpm().

Hope it helped :)

1

Fieldgroups aren't defined in the root of $form. It seems you should use not $form['group_mygroup'], but $form['#fieldgroups']['group_mygroup'].

0

You can look at the example module to see how they hide fieldsets. Based on that I think your code should be:

$form['group_mygroup']['#states'] = array(
  'invisible' => array(
    ':input[name="field_trigger"]' => array('value' => 'hide'),
  ),
),
1
  • 2
    Thanks for your suggestion. Though that's the same as my code only declared slightly different. I can hide fieldsets if I declare the form and fieldset using the FAPI. My issue is that it doesn't work with CCK field groups within hook_form_alter().
    – thepearson
    Commented Nov 4, 2011 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.