Add markup inside a link (in the renderable array) in preprocess field My question is for D8...

I have a field which is rendered as a link. Let say for simplicity that it will be rendered like this: &lt;a href="url"&gt;My Title&lt;/a&gt;

I would like to add a bootstrap icon inside the link to get a result like this: &lt;a href="url"&gt;&lt;span class="glyphicon glyphicon-info-sign" aria-hidden="true"&gt;&lt;/span&gt;My Title&lt;/a&gt;

I know that I can take the url and the title from within the preprocess function to reconstruct a new link (#markup)... But is it possible to simply add the icon code somewhere in the array to get the same result?

I am adding some more information to clarify:

1-The field which is rendered as a link is a term reference field rendered as a linked label

2-Here is my try in the preprocess_field function to achieve this

function mymodule_preprocess_field(&amp;$vars) {
  $fn = $vars['element']['#field_name'];
  switch ($fn) {
    case 'my_field':
      foreach ($vars['items'] as $key =&gt; $item) {
        $icon = '&lt;span class="glyphicon glyphicon-info-sign" aria-hidden="true"&gt;&lt;/span&gt;';
        $vars['items'][0]['content']['#title']=$icon.$item['content']['#title'];
      }
      break;
  }
}
Use the FormattableMarkup class for this:

$markup = new FormattableMarkup('&lt;a href="@url"&gt;&lt;span class="glyphicon glyphicon-info-sign" aria-hidden="true /&gt;&lt;/a&gt;', [
          '@url' =&gt; $url-&gt;toString(), // If $url is an Drupal\Core\Url object.
        ]);

$renderArray['your_element'] = $markup;


After edit:

Use the Markup class for this since your seem to be in control over your markup. If not use FormattableMarkup with placeholders for safety:

  function mymodule_preprocess_field(&amp;$vars) {
    $fn = $vars['element']['#field_name'];
    switch ($fn) {
      case 'my_field':
        foreach ($vars['items'] as $key =&gt; $item) {
          $icon = '&lt;span class="glyphicon glyphicon-info-sign" aria-hidden="true"&gt;&lt;/span&gt;';
          $uri = $item['content']['#title'];

          $formattedIcon = \Drupal\Core\Render\Markup::create($icon . $uri);

          $vars['items'][0]['content']['#title'] = $formattedIcon;
        }
        break;
    }
  }


From the FormattableMarkup docs:


  When cast to a string, this object replaces variable placeholders in
  the string with the arguments passed in during construction and
  escapes the values so they can be safely displayed as HTML. See the
  documentation of
  \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
  details on the supported placeholders and how to use them securely.
  Incorrect use of this class can result in security vulnerabilities.