You are here

BootstrapProgressBarShortcode.php in Bootstrap 3 shortcodes 8

Adds shortcodes for all Bootstrap elements.

File

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

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

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

/**
 * @Shortcode(
 *   id = "bs_progress_bar",
 *   token = "progress_bar",
 *   title = @Translation("Progress Bar"),
 *   description = @Translation("Bootstrap 3 shortcode"),
 *   weight = 50
 * )
 */
class BootstrapProgressBarShortcode extends ShortcodeBase {
  public function process(array $attributes, $content, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
    $attributes = $this
      ->getAttributes(array(
      'type' => '',
      'percent' => '',
      'label' => '',
      'xclass' => '',
      'data' => '',
    ), $attributes);
    $class = 'progress-bar';
    $class .= $attributes['type'] ? ' progress-bar-' . $attributes['type'] : '';
    $class .= $attributes['xclass'] ? ' ' . $attributes['xclass'] : '';
    $data_props = _bs_shortcodes_parse_data_attributes($attributes['data']);
    return sprintf('<div class="%s" role="progressbar" %s%s>%s</div>', Html::escape($class), $attributes['percent'] ? ' aria-value="' . (int) $attributes['percent'] . '" aria-valuemin="0" aria-valuemax="100" style="width: ' . (int) $attributes['percent'] . '%;"' : '', $data_props ? ' ' . $data_props : '', $attributes['percent'] ? sprintf('<span%s>%s</span>', !$attributes['label'] ? ' class="sr-only"' : '', (int) $attributes['percent'] . '% Complete') : '');
  }

  /**
   * {@inheritdoc}
   */
  public function tips($long = FALSE) {
    $output = array();
    $output[] = '<p><strong>[progress_bar percent="</strong>any number between 0 and 100<strong>"</strong> label="true, <u>false</u>" type="<u>default</u>, primary, success, info, warning, danger" xclass="any text" data="any text"<strong>]</strong>  ... <strong>[/progress_bar]</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#progress-bar-parameters',
      )) . '.</p>';
    }
    else {
      $output[] = t('More info <a href=":url" target="_blank">here</a>', array(
        ':url' => 'https://github.com/MWDelaney/bootstrap-3-shortcodes#progress-bar-parameters',
      )) . '.</p>';
    }
    return implode(' ', $output);
  }

}

Classes

Namesort descending Description
BootstrapProgressBarShortcode Plugin annotation @Shortcode( id = "bs_progress_bar", token = "progress_bar", title = @Translation("Progress Bar"), description = @Translation("Bootstrap 3 shortcode"), weight = 50 )