Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include html text inside a Drupal 8 generated link

I have a hard time to style a link via the Drupal 8 render structure. This link needs to be displayed in my custom module:

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

So between de tags I want a Font awesome icon in front of the text. But Drupal print all html out as readable text.

I also notice that the l() function is deprecated in Drupal 8. So what is the best way to do this in the Drupal 8 render structure?

like image 314
user3411864 Avatar asked Jun 06 '16 19:06

user3411864


People also ask

How do I add HTML code to Drupal?

HTML Page with Drupal regions is like any other content. You add the HTML content and it will be rendered in the content region of the theme. To add new HTML Page, go to '`/html-page/html_page`'. You can also get here by clicking on Structure from the Admin toolbar.

What is link text in Drupal?

This is a sandbox project, which contains experimental code for developer use only. Provides an additional formatter for the core Link field type in order to define a consistent link title using Manage Display. Options include: Use the field label as the link text.

How do I create a link in Drupal 8?

Add links inside a t() method. If you want to add a link inside the t() you can do what Drupal suggests on Dynamic or static links and HTML in translatable strings: use Drupal\Core\Url; $url = Url::fromRoute('entity.


1 Answers

If, like me, you wanted to use a render array of #type => 'link' and include an icon with it then you can do the following:

<?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'),
];
like image 112
joshmiller Avatar answered Sep 28 '22 02:09

joshmiller