You are here

public function BootstrapNavItemShortcode::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/BootstrapNavItemShortcode.php, line 24
Adds shortcodes for all Bootstrap elements.

Class

BootstrapNavItemShortcode
Plugin annotation @Shortcode( id = "bs_nav_item", token = "nav_item", title = @Translation("Nav item"), description = @Translation("Bootstrap 3 shortcode"), weight = 50 )

Namespace

Drupal\bs_shortcodes\Plugin\Shortcode

Code

public function process(array $attributes, $content, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
  $attributes = $this
    ->getAttributes(array(
    'link' => '',
    'active' => '',
    'disabled' => '',
    'dropdown' => '',
    'xclass' => '',
    'data' => '',
  ), $attributes);
  $li_classes = '';
  $li_classes .= $attributes['dropdown'] ? 'dropdown' : '';
  $li_classes .= $attributes['active'] != '' ? ' active' : '';
  $li_classes .= $attributes['disabled'] != '' ? ' disabled' : '';
  $a_classes = '';
  $a_classes .= $attributes['dropdown'] != '' ? ' dropdown-toggle' : '';
  $a_classes .= $attributes['xclass'] ? ' ' . $attributes['xclass'] : '';
  $data_props = _bs_shortcodes_parse_data_attributes($attributes['data']);

  # Wrong idea I guess ....

  #$pattern = ( $attributes['dropdown'] ) ? '<li%1$s><a href="%2$s"%3$s%4$s%5$s></a>%6$s</li>' : '<li%1$s><a href="%2$s"%3$s%4$s%5$s>%6$s</a></li>';

  //* If we have a dropdown shortcode inside the content we end the link before the dropdown shortcode, else all content goes inside the link
  $content = $attributes['dropdown'] ? str_replace('[dropdown]', '</a>[dropdown]', $content) : $content . '</a>';
  return sprintf('<li%1$s><a href="%2$s"%3$s%4$s%5$s>%6$s</li>', !empty($li_classes) ? sprintf(' class="%s"', Html::escape($li_classes)) : '', check_url($attributes['link']), !empty($a_classes) ? sprintf(' class="%s"', Html::escape($a_classes)) : '', $attributes['dropdown'] ? ' data-toggle="dropdown"' : '', $data_props ? ' ' . $data_props : '', $content);
}