MoreInfoTrait.php in Markdown 8.2
File
src/Traits/MoreInfoTrait.php
View source
<?php
namespace Drupal\markdown\Traits;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
trait MoreInfoTrait {
use RendererTrait;
protected function moreInfo($existing, $url, $label = 'More Info') {
if (!$url) {
return $existing;
}
if (!$url instanceof Url) {
$url = UrlHelper::isExternal($url) ? Url::fromUri($url)
->setOption('attributes', [
'target' => '_blank',
]) : Url::fromUserInput($url);
}
if (!$label instanceof MarkupInterface) {
$label = $this
->t($label);
}
$build = [
'#type' => 'link',
'#title' => new FormattableMarkup('[@label]', [
'@label' => $label,
]),
'#url' => $url,
'#options' => [
'attributes' => [
'class' => [
'installable-more-info',
],
'title' => $label,
],
],
'#prefix' => ' ',
];
$moreInfo = $this
->renderer()
->renderPlain($build);
if (empty($existing) || empty(trim($existing))) {
return $moreInfo;
}
return new FormattableMarkup('@existing @moreInfo', [
'@existing' => $existing,
'@moreInfo' => $moreInfo,
]);
}
}