You are here

public function Tabs::preRender in Field Group 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/field_group/FieldGroupFormatter/Tabs.php \Drupal\field_group\Plugin\field_group\FieldGroupFormatter\Tabs::preRender()

Allows the field group formatter to manipulate the field group array and attach the formatters rendering element.

Parameters

array $element: The field group render array.

object $rendering_object: The object / entity beïng rendered.

Overrides FieldGroupFormatterBase::preRender

File

src/Plugin/field_group/FieldGroupFormatter/Tabs.php, line 30

Class

Tabs
Plugin implementation of the 'horizontal_tabs' formatter.

Namespace

Drupal\field_group\Plugin\field_group\FieldGroupFormatter

Code

public function preRender(&$element, $rendering_object) {
  parent::preRender($element, $rendering_object);
  $element += array(
    '#prefix' => '<div class=" ' . implode(' ', $this
      ->getClasses()) . '">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#parents' => array(
      $this->group->group_name,
    ),
    '#default_tab' => '',
  );
  if ($this
    ->getSetting('id')) {
    $element['#id'] = Html::getId($this
      ->getSetting('id'));
  }

  // By default tabs don't have titles but you can override it in the theme.
  if ($this
    ->getLabel()) {
    $element['#title'] = Html::escape($this
      ->getLabel());
  }
  $form_state = new FormState();
  if ($this
    ->getSetting('direction') == 'vertical') {
    $element += array(
      '#type' => 'vertical_tabs',
      '#theme_wrappers' => array(
        'vertical_tabs',
      ),
    );
    $complete_form = array();
    $element = VerticalTabs::processVerticalTabs($element, $form_state, $complete_form);
  }
  else {
    $element += array(
      '#type' => 'horizontal_tabs',
      '#theme_wrappers' => array(
        'horizontal_tabs',
      ),
    );
    $on_form = $this->context == 'form';
    $element = HorizontalTabs::processHorizontalTabs($element, $form_state, $on_form);
  }

  // Make sure the group has 1 child. This is needed to succeed at form_pre_render_vertical_tabs().
  // Skipping this would force us to move all child groups to this array, making it an un-nestable.
  $element['group']['#groups'][$this->group->group_name] = array(
    0 => array(),
  );
  $element['group']['#groups'][$this->group->group_name]['#group_exists'] = TRUE;

  // Search for a tab that was marked as open. First one wins.
  foreach (Element::children($element) as $tab_name) {
    if (!empty($element[$tab_name]['#open'])) {
      $element[$this->group->group_name . '__active_tab']['#default_value'] = $tab_name;
      break;
    }
  }
}