public function BootstrapTabsShortcode::process in Bootstrap 3 shortcodes 8
Performs the shortcode processing.
Parameters
array $attributes: Array of attributes.
string $text: The text string to be processed.
string $langcode: The language code of the text to be filtered. Defaults to LANGCODE_NOT_SPECIFIED.
Return value
string The processed text.
Overrides ShortcodeInterface::process
File
- src/
Plugin/ Shortcode/ BootstrapTabsShortcode.php, line 29 - Adds shortcodes for all Bootstrap elements.
Class
- BootstrapTabsShortcode
- @Shortcode( id = "bs_tabs", token = "tabs", title = @Translation("Tabs"), description = @Translation("Bootstrap 3 shortcode"), weight = 50 )
Namespace
Drupal\bs_shortcodes\Plugin\ShortcodeCode
public function process(array $attributes, $content, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
$attributes = $this
->getAttributes(array(
'type' => '',
'xclass' => '',
'data' => '',
), $attributes);
$ul_class = 'nav';
$ul_class .= $attributes['type'] ? ' nav-' . $attributes['type'] : ' nav-tabs';
$ul_class .= $attributes['xclass'] ? ' ' . $attributes['xclass'] : '';
$div_class = 'tab-content';
$id = Html::cleanCssIdentifier('custom-tabs-' . $GLOBALS['tabs_count']);
$data_props = _bs_shortcodes_parse_data_attributes($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.
// 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"' : '', Html::cleanCssIdentifier('custom-tab-' . $GLOBALS['tabs_count'] . '-' . Html::escape($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>', Html::escape($ul_class), Html::escape($id), $data_props ? ' ' . $data_props : '', $tabs ? implode($tabs) : '', Html::escape($div_class), $content);
}