You are here

function bs_shortcodes_tabs in Bootstrap 3 shortcodes 7

bs_shortcodes_tabs

@author Filip Stefansson @since 1.0 Modified by TwItCh twitch@designweapon.com Now acts a whole nav/tab/pill shortcode solution!

File

./bs_shortcodes.module, line 1124

Code

function bs_shortcodes_tabs($attrs, $content = '') {
  extract(shortcode_attrs(array(
    "type" => '',
    "xclass" => '',
    "data" => '',
  ), $attrs));
  $ul_class = 'nav';
  $ul_class .= $type ? ' nav-' . $type : ' nav-tabs';
  $ul_class .= $xclass ? ' ' . $xclass : '';
  $div_class = 'tab-content';
  $id = drupal_html_class('custom-tabs-' . $GLOBALS['tabs_count']);
  $data_props = _bs_shortcodes_parse_data_attributes($data);

  // $attrs_map = bs_attribute_map( $content );
  // Because the Drupal API parses shortcodes from the inside out
  // we need to do some libxml magic here
  // ~Jur
  $previous_value = libxml_use_internal_errors(TRUE);
  $dom = new DOMDocument();
  $dom
    ->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
  libxml_clear_errors();
  libxml_use_internal_errors($previous_value);
  $finder = new DomXPath($dom);
  $domNodes = $finder
    ->query("//div[contains(@class, tab-pane)]");
  $attrs_map = array();
  foreach ($domNodes as $domNode) {
    if (strpos($domNode
      ->getAttribute('class'), 'active') === FALSE) {
      $active = FALSE;
    }
    else {
      $active = 'true';
    }
    $attrs_map[] = array(
      'tab' => array(
        'title' => $domNode
          ->getAttribute('title'),
        'active' => $active,
      ),
    );
  }

  // Extract the tab titles for use in the tab widget.
  if ($attrs_map) {
    $tabs = array();
    $GLOBALS['tabs_default_active'] = TRUE;
    foreach ($attrs_map as $check) {
      if (!empty($check["tab"]["active"])) {
        $GLOBALS['tabs_default_active'] = FALSE;
      }
    }
    $i = 0;
    foreach ($attrs_map as $tab) {
      $tabs[] = sprintf('<li%s><a href="#%s" data-toggle="tab">%s</a></li>', !empty($tab["tab"]["active"]) || $GLOBALS['tabs_default_active'] && $i == 0 ? ' class="active"' : '', drupal_html_class('custom-tab-' . $GLOBALS['tabs_count'] . '-' . check_plain($tab["tab"]["title"])), $tab["tab"]["title"]);
      $i++;
    }
  }
  if (isset($GLOBALS['tabs_count'])) {
    $GLOBALS['tabs_count']++;
  }
  else {
    $GLOBALS['tabs_count'] = 0;
  }
  return sprintf('<ul class="%s" id="%s"%s>%s</ul><div class="%s">%s</div>', check_plain($ul_class), check_plain($id), $data_props ? ' ' . $data_props : '', $tabs ? implode($tabs) : '', check_plain($div_class), $content);
}