You are here

BootstrapCollapseShortcode.php in Bootstrap 3 shortcodes 8

Adds shortcodes for all Bootstrap elements.

File

src/Plugin/Shortcode/BootstrapCollapseShortcode.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_collapse",
 *   token = "collapse",
 *   title = @Translation("Collapse"),
 *   description = @Translation("Bootstrap 3 shortcode"),
 *   weight = 50
 * )
 *
 * @author Filip Stefansson
 * @since 1.0
 */
class BootstrapCollapseShortcode extends ShortcodeBase {
  public function process(array $attributes, $content, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
    $attributes = $this
      ->getAttributes(array(
      'title' => '',
      'type' => '',
      'active' => '',
      'xclass' => '',
      'data' => '',
    ), $attributes);
    $panel_class = 'panel';
    $panel_class .= $attributes['type'] ? ' panel-' . $attributes['type'] : ' panel-default';
    $panel_class .= $attributes['xclass'] ? ' ' . $attributes['xclass'] : '';
    $collapse_class = 'panel-collapse';
    $collapse_class .= $attributes['active'] != '' ? ' in' : ' collapse';
    if (!isset($GLOBALS['collapsibles_count'])) {
      $GLOBALS['collapsibles_count'] = 0;
    }
    $parent = 'custom-collapse-' . $GLOBALS['collapsibles_count'];
    $current_collapse = $parent . '-' . Html::escape($attributes['title']);
    $data_props = _bs_shortcodes_parse_data_attributes($attributes['data']);
    return sprintf('<div class="%1$s"%2$s>
        <div class="panel-heading">
          <h4 class="panel-title">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#%3$s" href="#%4$s">%5$s</a>
          </h4>
        </div>
        <div id="%4$s" class="%6$s">
          <div class="panel-body">%7$s</div>
        </div>
      </div>', Html::escape($panel_class), $data_props ? ' ' . $data_props : '', $parent, Html::getUniqueId($current_collapse), $attributes['title'], Html::escape($collapse_class), $content);
  }

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

}

Classes

Namesort descending Description
BootstrapCollapseShortcode @Shortcode( id = "bs_collapse", token = "collapse", title = @Translation("Collapse"), description = @Translation("Bootstrap 3 shortcode"), weight = 50 )