You are here

BootstrapNavItemShortcode.php in Bootstrap 3 shortcodes 8

Adds shortcodes for all Bootstrap elements.

File

src/Plugin/Shortcode/BootstrapNavItemShortcode.php
View source
<?php

/**
 * @file
 * Adds shortcodes for all Bootstrap elements.
 */
namespace Drupal\bs_shortcodes\Plugin\Shortcode;

use Drupal\Component\Utility\Html;
use Drupal\Core\Language\Language;
use Drupal\shortcode\Plugin\ShortcodeBase;

/**
 * @Shortcode(
 *   id = "bs_nav_item",
 *   token = "nav_item",
 *   title = @Translation("Nav item"),
 *   description = @Translation("Bootstrap 3 shortcode"),
 *   weight = 50
 * )
 */
class BootstrapNavItemShortcode extends ShortcodeBase {
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function tips($long = FALSE) {
    $output = array();
    $output[] = '<p><strong>[nav_item</strong> link="any valid link" active="true, <u>false</u>" disabled="true, <u>false</u>"  dropdown="true, <u>false</u>" xclass="any text" data="any text"<strong>]</strong>  ... <strong>[/nav_item]</strong>';
    if ($long) {
      $output[] = t('More info about this shortcode <a href=":url" target="_blank">here</a>', array(
        ':url' => 'https://github.com/MWDelaney/bootstrap-3-shortcodes#nav-item-parameters',
      )) . '.</p>';
    }
    else {
      $output[] = t('More info <a href=":url" target="_blank">here</a>', array(
        ':url' => 'https://github.com/MWDelaney/bootstrap-3-shortcodes#nav-item-parameters',
      )) . '.</p>';
    }
    return implode(' ', $output);
  }

}

Classes

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