Fix for refactored field groups for D7 and added recursive functions

  1. /**
  2.  * Attach groups to the form.
  3.  */
  4. function field_group_attach_groups(&$form, $view_mode) {
  5.  
  6.   $entity_type = $form['#entity_type'];
  7.   $bundle = $form['#bundle'];
  8.  
  9.   $form['#groups'] = field_group_get_groups($entity_type,$bundle, $view_mode);
  10.  
  11.   // Create a lookup array.
  12.   $group_children = array();
  13.   foreach ($form['#groups'] as $group_name => $group) {
  14.     foreach ($group->children as $child) {
  15.       $group_children[$child] = $group_name;
  16.     }
  17.   }
  18.   $form['#group_children'] = $group_children;
  19.  
  20.   // Nest the fields in the corresponding field groups.
  21.   field_group_nest_fields($form, $form);
  22.  
  23. }
  24.  
  25. /**
  26.  * Recursive function to nest fields in the field groups.
  27.  * @param $form
  28.  * @return void
  29.  */
  30. function field_group_nest_fields(& $element, & $form, $parent = NULL) {
  31.  
  32.   $groups = $form['#groups'];
  33.   $group_fields = $form['#group_children'];
  34.  
  35.   foreach ($groups as $group_name => $group) {
  36.  
  37.     // Add fieldgroups to the current element.
  38.     if ((empty($group->parent_name) && !isset($parent)) || $parent == $group->parent_name) {
  39.       $element[$group_name] = array(
  40.         '#type' => 'fieldset',
  41.         '#title' => $group->label,
  42.         '#weight' => $group->weight,
  43.         '#collapsible' => TRUE,
  44.         '#collapsed' => FALSE,
  45.       );
  46.  
  47.       foreach ($group_fields as $groupfield => $groupname) {
  48.         // Stash the field in the group.
  49.         if ($groupname == $group_name && isset($form[$groupfield])) {
  50.           $element[$group_name][$groupname][$groupfield] = $form[$groupfield];
  51.           unset($form[$groupfield]);
  52.           unset($form['#group_children'][$groupfield]);
  53.         }
  54.       }
  55.  
  56.       unset($form['#groups'][$group_name]);
  57.       field_group_nest_fields($element[$group_name], $form, $group_name);
  58.  
  59.     }
  60.  
  61.   }
  62.  
  63. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.