You are here

public function Tabs::preRender in Field Group: Easy Responsive Tabs to Accordion 8

File

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

Class

Tabs
Plugin implementation of the 'ertta_tabs' formatter.

Namespace

Drupal\field_group_easy_responsive_tabs\Plugin\field_group\FieldGroupFormatter

Code

public function preRender(&$element, $rendering_object) {
  parent::preRender($element, $rendering_object);
  if (!empty($this
    ->getSetting('id'))) {
    $id = $this
      ->getSetting('id');
  }
  else {
    $class = implode('-', $this
      ->getClasses());
    $id = md5($class);
  }
  $label = $this
    ->t($this
    ->getLabel());
  $element += [
    '#type' => 'field_group_easy_responsive_tabs',
    // By default tabs don't have titles but you can override it in the theme.
    '#title' => !empty($label) ? Html::escape($label) : '',
    '#id' => Html::getId($id),
    '#theme_wrappers' => [
      'field_group_easy_responsive_tabs',
    ],
    '#parents' => [
      $this->group->parent_name,
    ],
    '#group_name' => !empty($element['#group_name']) ? $element['#group_name'] : $this->group->group_name,
    '#attributes' => [
      'id' => Html::getId($id),
      'class' => $this
        ->getClasses(),
    ],
    '#is_child' => TRUE,
  ];

  // Top level group.
  if (empty($element['#parents']) || $element['#parents'][0] == "") {
    $element['#is_child'] = FALSE;
  }
  $on_form = $this->context == 'form';

  // Add required JavaScript and Stylesheet.
  $element['#attached']['library'][] = 'field_group_easy_responsive_tabs/easy-responsive-tabs';
  $element['#attached']['library'][] = 'field_group_easy_responsive_tabs/easy-responsive-tabs-init';

  // Only add forms library on forms.
  if ($on_form) {
  }
  $settings = [
    'identifier' => Html::getId($id),
    'type' => $this
      ->getSetting('type'),
    'width' => $this
      ->getSetting('width'),
    'fit' => (bool) $this
      ->getSetting('fit'),
    'closed' => (bool) $this
      ->getSetting('closed'),
    'tabidentify' => Html::getId($id),
    'activetab_bg' => $this
      ->getSetting('active_bg'),
    'inactive_bg' => $this
      ->getSetting('inactive_bg'),
    'active_border_color' => $this
      ->getSetting('active_border_color'),
    'active_content_border_color' => $this
      ->getSetting('active_content_border_color'),
  ];
  foreach ($settings as $name => $value) {
    $element['#attributes']['data-' . $name] = $value;
  }
}