You are here

public static function VerticalTabs::preRenderGroup in Field Group 8.3

Pre render the group to support #group parameter.

Parameters

array $element: An associative array containing the properties and children of the element.

Return value

array The modified element with all group members.

File

src/Element/VerticalTabs.php, line 24

Class

VerticalTabs
Provides extra processing and pre rendering on the vertical tabs.

Namespace

Drupal\field_group\Element

Code

public static function preRenderGroup($element) {

  // The element may be rendered outside of a Form API context.
  if (!isset($element['#parents']) || !isset($element['#groups'])) {
    return $element;
  }
  if (isset($element['#group'])) {

    // Contains form element summary functionalities.
    $element['#attached']['library'][] = 'core/drupal.form';
    $group = $element['#group'];

    // If this element belongs to a group, but the group-holding element does
    // not exist, we need to render it (at its original location).
    if (!isset($element['#groups'][$group]['#group_exists'])) {

      // Intentionally empty to clarify the flow; we simply return $element.
    }
    elseif (!empty($element['#group_details'])) {

      // Intentionally empty to clarify the flow; we simply return $element.
    }
    elseif (Element::children($element['#groups'][$group])) {
      $element['#printed'] = TRUE;
    }
  }

  // Search for the correct default active tab.
  $group_identifier = implode('][', $element['#parents']);
  if (!empty($element['#groups'][$group_identifier])) {
    $children = Element::children($element['#groups'][$group_identifier], TRUE);
    foreach ($children as $key) {
      if (!empty($element['#groups'][$group_identifier][$key]['#open'])) {
        $element['#default_tab'] = $element['#groups'][$group_identifier][$key]['#id'];
        $element[str_replace('][', '__', $group_identifier) . '__active_tab']['#value'] = $element['#default_tab'];
      }
    }
  }
  return $element;
}