I need a link with another render array around it. More specifically I need a link around an image.

In the D7 days I'd just use a render array of type link and then set HTML to true and set the text as the image.

I was hoping to put it in a preprocess function.

  • Did you try to just pass in a render array? Not everywhere but often, that just works.
    – Berdir
    Commented Mar 3, 2016 at 19:33
  • If it doesn't, then rendering it and passing that in should work as that's then a Markup object and will not be further escaped.
    – Berdir
    Commented Mar 3, 2016 at 19:33

2 Answers 2


The HTML attribute removal was documented in change record #2932803, but the example uses the now deprecated SafeMarkup::format function. The FormattableMarkup class allows a non-deprecated way to do this. The API page has some good examples such as below:

Remember that this is has security implications, and to only use FormattableMarkup when you absolutely need it with strict placeholders.

$value = FormattableMarkup::placeholderFormat('<a href=":url">@variable</a>', [':url' => $url, @variable => $variable]);

There's probably a better way to do this without printing the anchor element and href attribute yourself.

  • Looks like this will be the solution. I was hoping to pass a render array inside the link though. I'll give it a go and feedback. Commented Mar 3, 2016 at 21:50
  • I recently discovered PlaceholderGenerator::createPlaceholder, which might allow that. I have not been able to get it to work inside an Input Filter. However it may actually work outside of an Input Filter as the tests suggest that it should.
    – mradcliffe
    Commented Mar 29, 2016 at 14:45

That's what worked for me. https://stackoverflow.com/a/44163527/1151303

    use Drupal\Core\Render\Markup;
    use Drupal\Core\Url;

    $form['actions']['reset_password'] = [
      '#type' => 'link',
      '#title' => Markup::create(
        '<span class="glyphicon glyphicon-cog"></span> Forgot / Reset Password'
      ), '#url' => Url::fromRoute('user.pass'),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.