Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Prerender / Link.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Prerender\Link.
5  */
6
7 namespace Drupal\bootstrap\Plugin\Prerender;
8
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;
14
15 /**
16  * Pre-render callback for the "link" element type.
17  *
18  * @ingroup plugins_prerender
19  *
20  * @BootstrapPrerender("link",
21  *   action = @BootstrapConstant(
22  *     "\Drupal\bootstrap\Bootstrap::CALLBACK_PREPEND"
23  *   )
24  * )
25  *
26  * @see \Drupal\Core\Render\Element\Link::preRenderLink()
27  */
28 class Link extends PrerenderBase {
29
30   /**
31    * {@inheritdoc}
32    */
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');
37
38       // Handle #icon_only.
39       if ($element->getProperty('icon_only')) {
40         if ($attribute_title = $element->getAttribute('title', '')) {
41           $title .= ' - ' . $attribute_title;
42         }
43         $element
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');
49         }
50         return;
51       }
52
53       // Handle #icon_position.
54       $position = $element->getProperty('icon_position', 'before');
55
56       // Render #icon and trim it (so it doesn't add underlines in whitespace).
57       $rendered_icon = trim(Element::create($icon)->renderPlain());
58
59       // Default position is before.
60       $markup = "$rendered_icon@title";
61       if ($position === 'after') {
62         $markup = "@title$rendered_icon";
63       }
64
65       // Replace the title and set an icon position class.
66       $element
67         ->setProperty('title', new FormattableMarkup($markup, ['@title' => $title]))
68         ->addClass("icon-$position");
69     }
70   }
71
72 }