Drupal - How can you make an HTML link?

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.


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

    <?php
    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'),
    ];

Tags:

Theming

8