You are here

function field_group_remove_empty_display_groups in Field Group 8

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

Remove empty groups on entity display.

Parameters

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

array $groups: Array of group objects.

1 call to field_group_remove_empty_display_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 793
Allows administrators to attach custom fields to fieldable types.

Code

function field_group_remove_empty_display_groups(&$element, $groups) {
  $empty_child = TRUE;
  $empty_group = TRUE;

  // Loop through the visible children for current element.
  foreach (Element::getVisibleChildren($element) as $name) {

    // Descend if the child is a group.
    if (in_array($name, $groups)) {
      $empty_child = field_group_remove_empty_display_groups($element[$name], $groups);
      if (!$empty_child) {
        $empty_group = FALSE;
      }
    }
    elseif (!empty($element[$name])) {
      $clone_element = $element[$name];

      // Weight parameter can make empty element seen as not empty.
      unset($clone_element['#weight']);
      if (!Element::isEmpty($clone_element)) {
        $empty_group = FALSE;
      }
    }
  }

  // Reset an empty group.
  if ($empty_group) {
    $element = [];
  }
  return $empty_group;
}