You are here

public static function HorizontalTabs::processHorizontalTabs in Field Group 8.3

Same name and namespace in other branches
  1. 8 src/Element/HorizontalTabs.php \Drupal\field_group\Element\HorizontalTabs::processHorizontalTabs()

Creates a group formatted as horizontal tabs.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

bool $on_form: Are the tabs rendered on a form or not.

Return value

array The processed element.

1 call to HorizontalTabs::processHorizontalTabs()
Tabs::preRender in src/Plugin/field_group/FieldGroupFormatter/Tabs.php
Allows the field group formatter to manipulate the field group array and attach the formatters rendering element.

File

src/Element/HorizontalTabs.php, line 104

Class

HorizontalTabs
Provides a render element for horizontal tabs.

Namespace

Drupal\field_group\Element

Code

public static function processHorizontalTabs(array &$element, FormStateInterface $form_state, $on_form = TRUE) {

  // Inject a new details as child, so that form_process_details() processes
  // this details element like any other details.
  $element['group'] = [
    '#type' => 'details',
    '#theme_wrappers' => [],
    '#parents' => $element['#parents'],
  ];

  // Add an invisible label for accessibility.
  if (!isset($element['#title'])) {
    $element['#title'] = t('Horizontal Tabs');
    $element['#title_display'] = 'invisible';
  }

  // Add required JavaScript and Stylesheet.
  $element['#attached']['library'][] = 'field_group/element.horizontal_tabs';

  // Only add forms library on forms.
  if ($on_form) {
    $element['#attached']['library'][] = 'core/drupal.form';
  }
  $name = implode('__', $element['#parents']);
  if ($form_state
    ->hasValue($name . '__active_tab')) {
    $element['#default_tab'] = $form_state
      ->getValue($name . '__active_tab');
  }
  $displayed_tab = isset($element['#default_tab']) ? $element['#default_tab'] : '';

  // The JavaScript stores the currently selected tab in this hidden
  // field so that the active tab can be restored the next time the
  // form is rendered, e.g. on preview pages or when form validation
  // fails.
  $element['#default_tab'] = $displayed_tab;
  $element[$name . '__active_tab'] = [
    '#type' => 'hidden',
    '#default_value' => $element['#default_tab'],
    '#attributes' => [
      'class' => [
        'horizontal-tabs-active-tab',
      ],
    ],
  ];
  return $element;
}