file Link.php

Namespace

Drupal\bootstrap\Plugin\Prerender
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Prerender;
  3. use Drupal\bootstrap\Bootstrap;
  4. use Drupal\bootstrap\Utility\Element;
  5. use Drupal\Component\Render\FormattableMarkup;
  6. /**
  7. * Pre-render callback for the "link" element type.
  8. *
  9. * @ingroup plugins_prerender
  10. *
  11. * @BootstrapPrerender("link",
  12. * action = @BootstrapConstant(
  13. * "\Drupal\bootstrap\Bootstrap::CALLBACK_PREPEND"
  14. * )
  15. * )
  16. *
  17. * @see \Drupal\Core\Render\Element\Link::preRenderLink()
  18. */
  19. class Link extends PrerenderBase {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function preRenderElement(Element $element) {
  24. // Injects the icon into the title (the only way this is possible).
  25. if ($icon = &$element->getProperty('icon')) {
  26. $title = $element->getProperty('title');
  27. // Handle #icon_only.
  28. if ($element->getProperty('icon_only')) {
  29. if ($attribute_title = $element->getAttribute('title', '')) {
  30. $title .= ' - ' . $attribute_title;
  31. }
  32. $element
  33. ->setAttribute('title', $title)
  34. ->addClass('icon-only')
  35. ->setProperty('title', $icon);
  36. if (Bootstrap::getTheme()->getSetting('tooltip_enabled')) {
  37. $element->setAttribute('data-toggle', 'tooltip');
  38. }
  39. return;
  40. }
  41. // Handle #icon_position.
  42. $position = $element->getProperty('icon_position', 'before');
  43. // Render #icon and trim it (so it doesn't add underlines in whitespace).
  44. $rendered_icon = trim(Element::create($icon)->renderPlain());
  45. // Default position is before.
  46. $markup = "[email protected]";
  47. if ($position === 'after') {
  48. $markup = "@title$rendered_icon";
  49. }
  50. // Replace the title and set an icon position class.
  51. $element
  52. ->setProperty('title', new FormattableMarkup($markup, ['@title' => $title]))
  53. ->addClass("icon-$position");
  54. }
  55. }
  56. }

Classes

Name Description
Link Pre-render callback for the "link" element type.