Jul 13, 2018 in Drupal 8, Snippets

Always try to use the core functions for linking inside custom formatters or modules. The following snippet allows html inside the link text.

use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Render\Markup;
$linkText = 'My link <br> containing tags';
$linkMarkup = Markup::create($linkText);
$link = Link::fromTextAndUrl($linkMarkup, $url);
$link = $link->toRenderable();