8.3.x Drupal.php Drupal::l($text, Url $url)
8.0.x Drupal.php public static Drupal::l($text, Url $url)
8.1.x Drupal.php public static Drupal::l($text, Url $url)
8.2.x Drupal.php public static Drupal::l($text, Url $url)
8.4.x Drupal.php public static 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.

Parameters

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.

Deprecated

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

\Drupal\Core\Utility\LinkGeneratorInterface::generate()

\Drupal\Core\Url

60 calls to Drupal::l()
block_help in core/modules/block/block.module
Implements hook_help().
BookTest::checkBookNode in core/modules/book/src/Tests/BookTest.php
Checks the outline of sub-pages; previous, up, and next.
CommentLinksTest::testLinkApprove in core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php
Test the comment approve link.
CommentLinksTest::testLinkReply in core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php
Test the comment reply link.
DbLogTest::verifyLinkEscaping in core/modules/dblog/src/Tests/DbLogTest.php
Tests the escaping of links in the operation row of a database log detail page.

... See full list

File

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

Class

Drupal
Static Service Container wrapper.

Code

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

Comments

kienan91’s picture

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

sushilpal’s picture

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

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

Please follow the below link for more details.

http://www.lohmeyer.rocks/blog/2015/12/03/0071-creating-links-custom-cla...

Thanks
S

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

IT SHOULD NOT BE DIFFICULT TO CREATE A LINK!

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));