Took me a while, but this should work. Your code has a $url
variable which may work fine, but this code below also shows how i got my $url
.
$url = new Url(
'entity.eab_contact_entity.edit_form', array(
'eab_contact_entity' => $entity->id(),
)
);
$icon_text = $this->t('<i class="fa fa-pencil"></i>');
$edit_link = \Drupal::service('link_generator')->generate($icon_text, $url);
It turns out that any text that goes into a link needs to be 'safe', so that malicious code cannot be injected etc. If you're curious, it was discussed at length here: https://www.drupal.org/node/2273923
The main point, though, and what makes the above code work for me and answers your question, is the $this->t()
surrounding the font-awesome string '<i class="fa fa-pencil"></i>'
. That renders it 'safe' and the link generated has the HTML we want, rather than just printing out the text of the HTML.
Finally, in case you are looking for help generating your URL, this tutorial has a lot of hints.