font awesome - Include html text inside a Drupal 8 generated link -


i have hard time style link via drupal 8 render structure. link needs displayed in custom module:

$add_link = \drupal::l('<i class="fa fa-cog"></i>' . t('add new project'), $url); 

so between de tags want font awesome icon in front of text. drupal print html out readable text.

i notice l() function deprecated in drupal 8. best way in drupal 8 render structure?

took me while, should work. code has $url variable may work fine, code below shows how got $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 text goes link needs 'safe', malicious code cannot injected etc. if you're curious, discussed @ length here: https://www.drupal.org/node/2273923

the main point, though, and makes above code work me , answers question, $this->t() surrounding font-awesome string '<i class="fa fa-pencil"></i>'. renders 'safe' , link generated has html want, rather printing out text of html.

finally, in case looking generating url, this tutorial has lot of hints.