You are here

BootstrapButtonShortcode.php in Bootstrap 3 shortcodes 8

Adds shortcodes for all Bootstrap elements.

File

src/Plugin/Shortcode/BootstrapButtonShortcode.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\Utility\UrlHelper;
use Drupal\Core\Language\Language;
use Drupal\shortcode\Plugin\ShortcodeBase;

/**
 * @Shortcode(
 *   id = "bs_button",
 *   token = "button",
 *   title = @Translation("Button"),
 *   description = @Translation("Bootstrap 3 shortcode"),
 *   weight = 50 
 * )
 * @author M. W. Delaney
 */
class BootstrapButtonShortcode extends ShortcodeBase {
  public function process(array $attributes, $content, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
    $attributes = $this
      ->getAttributes(array(
      'type' => '',
      'style' => '',
      'size' => '',
      'block' => '',
      'dropdown' => '',
      'link' => '',
      'target' => '',
      'disabled' => '',
      'active' => '',
      'xclass' => '',
      'title' => '',
      'data' => '',
    ), $attributes);
    $class = 'btn';
    $class .= $attributes['type'] ? ' btn-' . $attributes['type'] : ' btn-default';
    $class .= $attributes['size'] ? ' btn-' . $attributes['size'] : '';
    $class .= $attributes['block'] != '' ? ' btn-block' : '';
    $class .= $attributes['dropdown'] != '' ? ' dropdown-toggle' : '';
    $class .= $attributes['disabled'] != '' ? ' disabled' : '';
    $class .= $attributes['active'] != '' ? ' active' : '';
    $class .= $attributes['xclass'] ? ' ' . $attributes['xclass'] : '';

    // Styles:
    // Transparent
    // 3d
    // 3d-plus
    // swag see also http://tympanus.net/Development/CreativeButtons/
    $data_props = _bs_shortcodes_parse_data_attributes($attributes['data']);
    return sprintf('<a href="%s" class="%s"%s%s%s>%s</a>', UrlHelper::stripDangerousProtocols($attributes['link']), Html::escape($class), $attributes['target'] ? sprintf(' target="%s"', Html::escape($attributes['target'])) : '', $attributes['title'] ? sprintf(' title="%s"', Html::escape($attributes['title'])) : '', $data_props ? ' ' . $data_props : '', $content);
  }

  /**
   * {@inheritdoc}
   */
  public function tips($long = FALSE) {
    $output = array();
    $output[] = '<p><strong>[button</strong> type="<u>default</u>, primary, success, info, warning, danger, link" size="xs, sm, lg" block="true, <u>false</u>" dropdown="true, <u>false</u>" active="true, <u>false</u>" disabled="true, <u>false</u>" link="any valid link" target="any valid target" xclass="any text" data="any text"<strong>]</strong>  ... <strong>[/button]</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#buttons',
      )) . '.</p>';
    }
    else {
      $output[] = t('More info <a href=":url" target="_blank">here</a>', array(
        ':url' => 'https://github.com/MWDelaney/bootstrap-3-shortcodes#buttons',
      )) . '.</p>';
    }
    return implode(' ', $output);
  }

}

Classes

Namesort descending Description
BootstrapButtonShortcode @Shortcode( id = "bs_button", token = "button", title = @Translation("Button"), description = @Translation("Bootstrap 3 shortcode"), weight = 50 ) @author M. W. Delaney