You are here

BootstrapPanelShortcode.php in Bootstrap 3 shortcodes 8

Adds shortcodes for all Bootstrap elements.

File

src/Plugin/Shortcode/BootstrapPanelShortcode.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_panel",
 *   token = "panel",
 *   title = @Translation("Panel"),
 *   description = @Translation("Bootstrap 3 shortcode"),
 *   weight = 50
 * )
 */
class BootstrapPanelShortcode extends ShortcodeBase {
  public function process(array $attributes, $content, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
    $attributes = $this
      ->getAttributes(array(
      'title' => '',
      'heading' => '',
      'type' => '',
      'footer' => '',
      'xclass' => '',
      'data' => '',
    ), $attributes);
    $class = 'panel';
    $class .= $attributes['type'] ? ' panel-' . $attributes['type'] : ' panel-default';
    $class .= $attributes['xclass'] ? ' ' . $attributes['xclass'] : '';
    if (!$attributes['heading'] && $attributes['title']) {
      $attributes['heading'] = $attributes['title'];
      $attributes['title'] = TRUE;
    }
    $data_props = _bs_shortcodes_parse_data_attributes($attributes['data']);
    $attributes['footer'] = $attributes['footer'] ? '<div class="panel-footer">' . $attributes['footer'] . '</div>' : '';
    if ($attributes['heading']) {
      $attributes['heading'] = sprintf('<div class="panel-heading">%s%s%s</div>', $attributes['title'] ? '<h3 class="panel-title">' : '', check_markup($attributes['heading']), $attributes['title'] ? '</h3>' : '');
    }
    else {
      $attributes['heading'] = '';
    }
    return sprintf('<div class="%s"%s>%s<div class="panel-body">%s</div>%s</div>', Html::escape($class), $data_props ? ' ' . $data_props : '', $attributes['heading'], $content, $attributes['footer'] ? ' ' . $attributes['footer'] : '');
  }

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

}

Classes

Namesort descending Description
BootstrapPanelShortcode Plugin annotation @Shortcode( id = "bs_panel", token = "panel", title = @Translation("Panel"), description = @Translation("Bootstrap 3 shortcode"), weight = 50 )