plus-8.x-4.x-dev/src/Plugin/Theme/Template/Link.php
src/Plugin/Theme/Template/Link.php
<?php
namespace Drupal\plus\Plugin\Theme\Template;
use Drupal\plus\Plugin\ThemePluginBase;
use Drupal\plus\Utility\Element;
use Drupal\Component\Render\FormattableMarkup;
/**
* Pre-render callback for the "link" element.
*
* @ingroup plugins_template
*
* @Template("link",
* placement = "prepend",
* )
*
* @see \Drupal\Core\Render\Element\Link::preRenderLink()
*/
class Link extends ThemePluginBase implements PrerenderInterface {
/**
* {@inheritdoc}
*/
public function preRender(Element $element) {
// Injects the icon into the title (the only way this is possible).
if ($icon = &$element->getProperty('icon')) {
$title = $element->getProperty('title');
// Handle #icon_only.
if ($element->getProperty('icon_only')) {
if ($attribute_title = $element->getAttribute('title', '')) {
$title .= ' - ' . $attribute_title;
}
$element
->setAttribute('title', $title)
->addClass('icon-only')
->setProperty('title', $icon);
if ($this->theme->getSetting('tooltip_enabled')) {
$element->setAttribute('data-toggle', 'tooltip');
}
return;
}
// Handle #icon_position.
$position = $element->getProperty('icon_position', 'before');
// Render #icon and trim it (so it doesn't add underlines in whitespace).
$rendered_icon = trim(Element::reference($icon)->renderPlain());
// Default position is before.
$markup = "$rendered_icon@title";
if ($position === 'after') {
$markup = "@title$rendered_icon";
}
// Replace the title and set an icon position class.
$element
->setProperty('title', new FormattableMarkup($markup, ['@title' => $title]))
->addClass("icon-$position");
}
}
}
