4 * Contains \Drupal\bootstrap\Plugin\Prerender\Link.
7 namespace Drupal\bootstrap\Plugin\Prerender;
9 use Drupal\bootstrap\Annotation\BootstrapConstant;
10 use Drupal\bootstrap\Annotation\BootstrapPrerender;
11 use Drupal\bootstrap\Bootstrap;
12 use Drupal\bootstrap\Utility\Element;
13 use Drupal\Component\Render\FormattableMarkup;
16 * Pre-render callback for the "link" element type.
18 * @ingroup plugins_prerender
20 * @BootstrapPrerender("link",
21 * action = @BootstrapConstant(
22 * "\Drupal\bootstrap\Bootstrap::CALLBACK_PREPEND"
26 * @see \Drupal\Core\Render\Element\Link::preRenderLink()
28 class Link extends PrerenderBase {
33 public static function preRenderElement(Element $element) {
34 // Injects the icon into the title (the only way this is possible).
35 if ($icon = &$element->getProperty('icon')) {
36 $title = $element->getProperty('title');
39 if ($element->getProperty('icon_only')) {
40 if ($attribute_title = $element->getAttribute('title', '')) {
41 $title .= ' - ' . $attribute_title;
44 ->setAttribute('title', $title)
45 ->addClass('icon-only')
46 ->setProperty('title', $icon);
47 if (Bootstrap::getTheme()->getSetting('tooltip_enabled')) {
48 $element->setAttribute('data-toggle', 'tooltip');
53 // Handle #icon_position.
54 $position = $element->getProperty('icon_position', 'before');
56 // Render #icon and trim it (so it doesn't add underlines in whitespace).
57 $rendered_icon = trim(Element::create($icon)->renderPlain());
59 // Default position is before.
60 $markup = "$rendered_icon@title";
61 if ($position === 'after') {
62 $markup = "@title$rendered_icon";
65 // Replace the title and set an icon position class.
67 ->setProperty('title', new FormattableMarkup($markup, ['@title' => $title]))
68 ->addClass("icon-$position");