ButtonShortcode.php in Shortcode 2.0.x
File
shortcode_basic_tags/src/Plugin/Shortcode/ButtonShortcode.php
View source
<?php
namespace Drupal\shortcode_basic_tags\Plugin\Shortcode;
use Drupal\Core\Language\Language;
use Drupal\shortcode\Plugin\ShortcodeBase;
class ButtonShortcode extends ShortcodeBase {
public function process(array $attributes, $text, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
$attributes = $this
->getAttributes([
'path' => '<front>',
'url' => '',
'title' => '',
'class' => '',
'id' => '',
'style' => '',
'media_file_url' => FALSE,
], $attributes);
$url = $attributes['url'];
if (empty($url)) {
$url = $this
->getUrlFromPath($attributes['path'], $attributes['media_file_url']);
}
$title = $this
->getTitleFromAttributes($attributes['title'], $text);
$class = $this
->addClass($attributes['class'], 'button');
$element_attributes = [
'href' => $url,
'class' => $class,
'id' => $attributes['id'],
'style' => $attributes['style'],
'title' => $title,
];
$element_attributes = array_filter($element_attributes);
$output = [
'#theme' => 'shortcode_button',
'#url' => $url,
'#attributes' => $element_attributes,
'#text' => $text,
];
return $this
->render($output);
}
public function tips($long = FALSE) {
$output = [];
$output[] = '<p><strong>' . $this
->t('[button path="path" (class="additional class")]text[/button]') . '</strong> ';
if ($long) {
$output[] = $this
->t('Inserts a link formatted like as a button. The <em>path</em> parameter provides the link target (the default is the front page).
The <em>title</em> will be formatted as a link title (small tooltip over the link - helps for SEO).
Additional class names can be added by the <em>class</em> parameter.') . '</p>';
}
else {
$output[] = $this
->t('Inserts a link formatted as a button. Use the url parameter for the link.') . '</p>';
}
return implode(' ', $output);
}
}