You are here

class BootstrapQuickTabs in Bootstrap Quick Tabs 8

Provides a 'Bootstrap Tabs' tab renderer.

Plugin annotation


@TabRenderer(
  id = "bootstrap_tabs",
  name = @Translation("bootstrap tabs"),
)

Hierarchy

Expanded class hierarchy of BootstrapQuickTabs

File

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

Namespace

Drupal\bootstrap_quicktabs\Plugin\TabRenderer
View source
class BootstrapQuickTabs extends TabRendererBase {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function optionsForm(QuickTabsInstance $instance) {
    $options = $instance
      ->getOptions()['bootstrap_tabs'];
    $renderer = $instance
      ->getRenderer();
    $form = [];
    $url = Url::fromUri('https://getbootstrap.com/docs/3.3/components/#nav');
    $link = Link::fromTextAndUrl($this
      ->t('Bootstrap'), $url)
      ->toString();
    $form['tabstyle'] = [
      '#default_value' => $renderer == 'bootstrap_tabs' && isset($options['tabstyle']) && $options['tabstyle'] ? $options['tabstyle'] : 'tabs',
      '#description' => $this
        ->t('Choose the style of tab to use from %bootstrap', [
        '%bootstrap' => $link,
      ]),
      '#options' => [
        'tabs' => $this
          ->t('Tabs'),
        'pills' => $this
          ->t('Pills'),
      ],
      '#title' => $this
        ->t('Style of tabs'),
      '#type' => 'radios',
    ];
    $url = Url::fromUri('https://getbootstrap.com/docs/3.3/components/#nav');
    $link = Link::fromTextAndUrl($this
      ->t('Bootstrap'), $url)
      ->toString();
    $form['tabposition'] = [
      '#default_value' => $renderer == 'bootstrap_tabs' && isset($options['tabposition']) && $options['tabposition'] ? $options['tabposition'] : 'basic',
      '#description' => $this
        ->t('Choose the position of tab to use from %bootstrap', [
        '%bootstrap' => $link,
      ]),
      '#options' => [
        'basic' => $this
          ->t('Tabs/pills on the top'),
        'left' => $this
          ->t('Tabs/pills on the left'),
        'right' => $this
          ->t('Tabs /pills on the right'),
        'below' => $this
          ->t('Tabs/pills on the bottom'),
        'justified' => $this
          ->t('Tabs/pills justified on the top'),
        'stacked' => $this
          ->t('Tabs/pills stacked'),
      ],
      '#title' => $this
        ->t('Position of tabs'),
      '#type' => 'radios',
    ];
    $url = Url::fromUri('https://getbootstrap.com/docs/3.3/javascript/#tabs-examples');
    $link = Link::fromTextAndUrl($this
      ->t('Bootstrap'), $url)
      ->toString();
    $default_effects = [];
    if ($renderer == 'bootstrap_tabs' && isset($options['tabeffects']) && $options['tabeffects']) {
      $default_effects[] = 'fade';
    }
    $form['tabeffects'] = [
      '#default_value' => $default_effects,
      '#description' => $this
        ->t('Select fade effect on or off %bootstrap', [
        '%bootstrap' => $link,
      ]),
      '#options' => [
        'fade' => $this
          ->t('Fade'),
      ],
      '#title' => $this
        ->t('Effect of tabs'),
      '#type' => 'checkboxes',
    ];
    return $form;
  }

  /**
   * Returns a render array to be used in a block or page.
   *
   * @return array
   *   a render array
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BootstrapQuickTabs::optionsForm public function Return form elements used on the edit/add from. Overrides TabRendererBase::optionsForm
BootstrapQuickTabs::render public function Returns a render array to be used in a block or page. Overrides TabRendererBase::render
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TabRendererBase::getName public function Gets the name of the plugin.