You are here

function field_group_remove_empty_form_groups in Field Group 8.3

Same name and namespace in other branches
  1. 8 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

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

array $groups: Array of group objects.

string $entity_type: The entity type.

2 calls 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().
field_group_field_group_form_process_build_alter in ./field_group.module
Implements hook_field_group_form_process_build_alter().

File

./field_group.module, line 1044
Allows administrators to attach field groups.

Code

function field_group_remove_empty_form_groups(&$element, $groups, $entity_type) {
  $exceptions = [
    'user__account',
    'comment__author',
  ];
  $children = Element::getVisibleChildren($element);
  $empty_groups_indication = array_fill_keys($groups, TRUE);
  if (count($children)) {
    foreach ($children as $childname) {
      $exception = $entity_type . '__' . $childname;
      $empty_element = !(isset($element[$childname]['#type']) || isset($element[$childname]['#markup']) || in_array($exception, $exceptions));

      // If the element is not empty, and it has a group. Mark the group as not
      // empty.
      if (!$empty_element && isset($element[$childname]['#group']) && (!isset($element[$childname]['#access']) || $element[$childname]['#access'])) {
        $name_prefix = implode('][', $element['#array_parents']) . '][';
        $group_name = str_replace($name_prefix, '', $element[$childname]['#group']);
        $empty_groups_indication[$group_name] = FALSE;
      }
      $show_empty_fields = isset($element[$childname]['#show_empty_fields']) && $element[$childname]['#show_empty_fields'];
      if ($show_empty_fields) {
        $empty_groups_indication[$childname] = FALSE;
      }
    }
  }

  // Set access to false for all empty groups.
  $empty_groups = array_filter($empty_groups_indication);
  foreach (array_keys($empty_groups) as $group_name) {
    $element[$group_name]['#access'] = FALSE;
  }
}