8.5.x Drupal.php Drupal::l($text, Url $url)
8.0.x Drupal.php Drupal::l($text, Url $url)
8.1.x Drupal.php Drupal::l($text, Url $url)
8.2.x Drupal.php Drupal::l($text, Url $url)
8.3.x Drupal.php Drupal::l($text, Url $url)
8.4.x Drupal.php Drupal::l($text, Url $url)
8.6.x Drupal.php Drupal::l($text, Url $url)

Renders a link with a given link text and Url object.

This method is a convenience wrapper for the link generator service's generate() method.


string $text: The link text for the anchor tag.

\Drupal\Core\Url $url: The URL object used for the link.

Return value

\Drupal\Core\GeneratedLink A GeneratedLink object containing a link to the given route and parameters and bubbleable metadata.


in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Link instead. Example:

$link = Link::fromTextAndUrl($text, $url);

See also




core/lib/Drupal.php, line 580
Contains \Drupal.


Static Service Container wrapper.


public static function l($text, Url $url) {
  return static::getContainer()
    ->generate($text, $url);


kienan91’s picture

In Drupal 8 used Drupal::l() only pass text and url. So how to add attributes ?

sushilpal’s picture

please see below the way to add attribute in l().
use Drupal\Core\Url;

t('My Button'),
new Url('my.link.route',
['route_parameter' => $custom_variable],
['attributes' => ['class' => ['button']]])

Please follow the below link for more details.



seanr’s picture

That's just plain obnoxious - they should have kept l() as a wrapper for this. Furthermore, \Drupal::l() is now deprecated as well.

hyperlinked’s picture

You can say that again. Ugh.

kienan91’s picture

Thanks for your comment !

dahousecat’s picture

I just get a fatal error class not found when I use this function.

I think I need to add something like use Drupal\Core\Link to the top of the file but how do we go about finding out what path to use with the use?

birdsnbees’s picture

This reference helped me: Drupal 8 Link creation with query, fragment and attributes examples - https://www.webfoobar.com/node/67

kienan91’s picture

Hi , How to format text into \Drupal::l() to HTML ?

thanhxt’s picture

l('E-Mail', 'mailto:test@gmail.com', array(absolute => TRUE)). How convert to drupal 8. Thanks

jiang zhan’s picture

You can try this code
use Drupal\Core\Url;
$l = \Drupal::l(t('E-Mail'), Url::fromUri('mailto:' . $user->getEmail()));
You can put Options array inside Url::fromUri($uri,$options = []);

For Internal link (Create by Drupal)
$l = \Drupal::l(t('Internal Link'), Url::fromUri('internal:/node/7'));

For Internal link (Create by yourself)
$l = \Drupal::l(t('Router'), Url::fromRoute('router.name'));
Router.name is inside your MODULE/MODULE.routing.yml you also need a Controller. It's like you create a 'homepage' by using hook_menu in Drupal 7.

For external Link
$l = \Drupal::l(t('External Link'), Url::fromUri('http://www.google.com'));

zkent’s picture


pmunch’s picture

Thanks for the code snippets, they really help.
But I fully agree with @zkent, creating a link shoudn't be that complicated...
Or most of us may create its own wrapper.

rOprOprOp’s picture

use Drupal\Core\Url;
use Drupal\Core\Link;

$link = Link::fromTextAndUrl(t('KISS'), Url::fromUri('http://google.com',array()))->toString();
echo t('gimme a @link', array('@link' => $link));