You are here

public function BootstrapQuickTabs::render in Bootstrap Quick Tabs 8

Returns a render array to be used in a block or page.

Return value

array a render array

Overrides TabRendererBase::render

File

src/Plugin/TabRenderer/BootstrapQuickTabs.php, line 86

Class

BootstrapQuickTabs
Provides a 'Bootstrap Tabs' tab renderer.

Namespace

Drupal\bootstrap_quicktabs\Plugin\TabRenderer

Code

public function render(QuickTabsInstance $instance) {
  $qt_id = $instance
    ->id();
  $type = \Drupal::service('plugin.manager.tab_type');
  $settings = $instance
    ->getOptions()['bootstrap_tabs'];
  $is_ajax = isset($settings['ajax']) && $settings['ajax'];
  $classes = [
    'nav',
    'nav-' . $settings['tabstyle'],
  ];
  if ($settings['tabposition'] == 'justified' || $settings['tabposition'] == 'stacked') {
    $classes[] = ' nav-' . $settings['tabposition'];
  }
  $build = [
    '#theme' => 'bootstrap_tabs',
    '#options' => [
      'classes' => implode(' ', $classes),
      'style' => $settings['tabstyle'],
      'position' => $settings['tabposition'],
      'effects' => $settings['tabeffects'],
      'ajax' => $is_ajax,
    ],
    '#id' => $qt_id,
  ];
  $build['#theme_wrappers'] = [
    'container' => [
      '#attributes' => [
        'class' => $settings['tabposition'] != 'basic' && $settings['tabposition'] != 'stacked' && $settings['tabposition'] != 'justified' ? ' tabs-' . $settings['tabposition'] : '',
        'id' => 'quicktabs-container-' . $qt_id,
      ],
    ],
  ];

  // Pages of content that will be shown or hidden.
  $tab_pages = [];

  // Tabs used to show/hide content.
  $titles = [];
  foreach ($instance
    ->getConfigurationData() as $index => $tab) {
    $default_tab = $instance
      ->getDefaultTab() == 9999 ? 0 : $instance
      ->getDefaultTab();
    if (!$is_ajax || $default_tab == $index) {
      $object = $type
        ->createInstance($tab['type']);
      $render = $object
        ->render($tab);
    }
    else {
      $render = [
        '#markup' => 'Loading content ...',
      ];
    }

    // If user wants to hide empty tabs and there is no content
    // then skip to next tab.
    if ($instance
      ->getHideEmptyTabs() && empty($render)) {
      continue;
    }
    $classes = [
      'tab-pane',
    ];
    if ($default_tab == $index) {
      $classes[] = 'active';
    }
    if (isset($settings['tabeffects']) && in_array('fade', $settings['tabeffects'], TRUE)) {
      $classes[] = 'fade';
      if ($default_tab == $index) {
        $classes[] = 'in';
      }
    }
    $block_id = 'quicktabs-tabpage-' . $qt_id . '-' . $index;
    $tab_pages[$block_id] = [
      'content' => render($render),
      'classes' => implode(' ', $classes),
    ];
    $link_classes = [];
    if ($default_tab == $index) {
      $link_classes[] = 'quicktabs-loaded active';
    }
    elseif ($is_ajax) {
      $link_classes[] = 'use-ajax';
    }
    else {
      $link_classes[] = 'quicktabs-loaded';
    }
    $titles[$block_id] = [
      'title' => new TranslatableMarkup($tab['title']),
      'classes' => implode(' ', $link_classes),
    ];
  }
  $build['#content'] = $tab_pages;
  $build['#tabs'] = $titles;
  $build['#attached'] = [
    'library' => [
      'bootstrap_quicktabs/bootstrap_tabs',
    ],
  ];
  return $build;
}