You are here

function field_group_remove_empty_form_groups in Field Group 8

Same name and namespace in other branches
  1. 8.3 field_group.module \field_group_remove_empty_form_groups()
  2. 7.2 field_group.module \field_group_remove_empty_form_groups()
  3. 7 field_group.module \field_group_remove_empty_form_groups()

Remove empty groups on forms.

Parameters

String $parent_name: The name of the element.

array $element: The element to check the empty state.

array $groups: Array of group objects.

1 call to field_group_remove_empty_form_groups()
field_group_field_group_build_pre_render_alter in ./field_group.module
Implements hook_field_group_build_pre_render_alter().

File

./field_group.module, line 746
Allows administrators to attach custom fields to fieldable types.

Code

function field_group_remove_empty_form_groups($name, &$element, $groups, &$form_groups, $entity) {
  $exceptions = array(
    'user__account',
    'comment__author',
  );
  $children = Element::children($element);
  $hasChildren = FALSE;
  if (count($children)) {
    foreach ($children as $childname) {
      if (in_array($childname, $groups)) {
        field_group_remove_empty_form_groups($childname, $element[$childname], $groups, $form_groups, $entity);
      }
      $exception = $entity . '__' . $childname;
      $hasChildren = $hasChildren ? TRUE : isset($element[$childname]['#type']) || isset($element[$childname]['#markup']) || in_array($exception, $exceptions);
    }
  }
  if (!$hasChildren) {

    // Remove empty elements from the #fieldgroups.
    if (empty($element) && isset($form_groups[$name]) && !is_array($form_groups[$name])) {
      foreach ($form_groups as $group_name => $group) {
        if (isset($group->children)) {
          $group_children = array_flip($group->children);
          if (isset($group_children[$name])) {
            unset($form_groups[$group_name]->children[$group_children[$name]]);
          }
        }
      }
    }
    $element['#access'] = FALSE;
  }
}