\l
function
Formats an internal or external URL link as an HTML anchor tag.

Formats an internal or external URL link as an HTML anchor tag.

This function correctly handles aliased paths and adds an 'active' class attribute to links that point to the current page (for theming), so all internal links output by modules should be generated by this function if possible.

However, for links enclosed in translatable text you should use t() and embed the HTML anchor tag directly in the translated string. For example:

t('Visit the <a href="@url">settings</a> page', array('@url' => url('admin')));

This keeps the context of the link title ('settings' in the example) for translators.

Comments

izus’s picture

you should use Drupal\Core\Url with Url::fromRoute or Url::fromUri methods.
Example from the change notice :

<?php
// Internal path (defined by a route in Drupal 8).
use Drupal\Core\Url;
$url = Url::fromRoute('book.admin');
$internal_link = \Drupal::l(t('Book admin'), $url);
// External Uri.
use Drupal\Core\Url;
$url = Url::fromUri('http://www.example.com/');
$external_link = \Drupal::l(t('External link'), $url);
?>

See the change notice : https://www.drupal.org/node/2346779

sime’s picture

djouuuuh’s picture

To make the path work in every template file, use file_create_url() like the theme_image function.

$src = file_create_url(path_to_theme() . '/path/to/image.png');
print l('<img src="' . $src . '" alt="Hello" title="Hello"/>', "http://www.example.com", array('html'=>TRUE));
Anonymous’s picture

With my link I was including a destination. When I did that though the resulting link transformed the ? to %3F and the = to %3D. So I found this link: http://stackoverflow.com/questions/15278864/convert-special-characters-t... and used the rawurldecode to solve it.

OLD:
print l("LOGIN","user/login/?destination=frontpage", array('attributes' => array('id' =>'button','class' => array('buttonlogin'))));

NEW:
print rawurldecode(rawurldecode(l("LOGIN","user/login/?destination=frontpage", array('attributes' => array('id' =>'button','class' => array('buttonlogin'))))));

Sorry the code isn't blocked in, don't know the html to do that and the help didn't say, or I was blind and missed it.

gradedcatfood’s picture

Is their a reason why this function does not implement html_entity_decode() or decode_entities()? Should be trying to convert everything to valid HTML.

kurtismccartney’s picture

What if we were looking for something like this:

< a href="/" property="item" typeof="WebPage" >< span property="name" >Home< /span >< /a >

The following does not deliver the internal span:
l($content, $it['url'], array('attributes' => array('property' => 'item', 'typeof' => 'WebPage')));

Pages