Drupal - Allow markup inside drupal 8 Link function
Use the Markup class:
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Render\Markup;
$url = Url::fromUri('http://www.example.com');
$linkText = 'My link <br> containing tags';
$linkMarkup = Markup::create($linkText);
$link = Link::fromTextAndUrl($linkMarkup, $url);
$link = $link->toRenderable();
If you build a render array for the link you can simply place the html markup in #markup
:
$build['my_link'] = [
'#type' => 'link',
'#title' => [
'#markup' => 'My link <br> containing tags',
],
'#url' => Url::fromUri('http://www.example.com'),
];