How to hide Field Groups in Drupal 7, the right way

There are times when you need to manipulate a form via hook_form_alter in Drupal, and you may need to hide/show fields depending on some criteria.

You can hide a single field using something like $form['field_name']['#type'] = 'hidden';.

But, what if you’re using the ‘field_group’ module and you want to hide a whole group? Well, you can use the function field_group_hide_field_groups(). It receives two parameters, $form, and an array of group names to hide.

i.e.

function MODULE_form_form_id_alter(&$form, &$form_state, $form_id) {
$groups_to_hide = [
'group_some_name',
'group_some_other_name'
];
field_group_hide_field_groups($form, $groups_to_hide);
}

 

So you don’t have to hide field by field anymore!


Tagged: ,