LinkShortcode.php in Shortcode 2.0.x
File
shortcode_basic_tags/src/Plugin/Shortcode/LinkShortcode.php
View source
<?php
namespace Drupal\shortcode_basic_tags\Plugin\Shortcode;
use Drupal\Core\Language\Language;
use Drupal\shortcode\Plugin\ShortcodeBase;
class LinkShortcode 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']);
}
if ($text) {
$title = $this
->getTitleFromAttributes($attributes['title'], $text);
$element_attributes = [
'href' => $url,
'class' => $attributes['class'],
'id' => $attributes['id'],
'style' => $attributes['style'],
'title' => $title,
];
$element_attributes = array_filter($element_attributes);
$output = [
'#theme' => 'shortcode_link',
'#url' => $url,
'#attributes' => $element_attributes,
'#text' => $text,
];
return $this
->render($output);
}
return $url;
}
public function tips($long = FALSE) {
$output = [];
$output[] = '<p><strong>' . $this
->t('[link path="the drupal path" (title="link title"|class="additional class"|id="item id"|style="css style rules")]text[/link]') . '</strong>';
if ($long) {
$output[] = $this
->t('Inserts an aliased drupal path around the text. You can omit the text and the closing [/link], you get back the url only.') . '</p>';
$output[] = '<p>' . $this
->t('Additional class names can be added by the <em>class</em> parameter. The id parameter gives the html an unique css id. In the <em>style</em> parameter you can use your own css definition.') . '</p>';
}
else {
$output[] = $this
->t('Inserts an aliased drupal path around the text. You can omit the text and the closing [/link], you get back the url only.') . '</p>';
}
return implode(' ', $output);
}
}
Classes
Name |
Description |
LinkShortcode |
Insert div or span around the text with some css classes. |