l
- Versions
- 4.6 – 5
l($text, $path,$attributes= array(),$query= NULL,$fragment= NULL,$absolute= FALSE,$html= FALSE)- 6
l($text, $path, $options = array())- 7
l($text, $path, array $options = array())
Format an internal Drupal link.
This function correctly handles aliased paths, and allows themes to highlight links to the current page correctly, so all internal links output by modules should be generated by this function if possible.
Parameters
$text The text to be enclosed with the anchor tag.
$path The Drupal path being linked to, such as "admin/content/node". Can be an external or internal URL.
- If you provide the full URL, it will be considered an external URL.
- If you provide only the path (e.g. "admin/content/node"), it is considered an internal link. In this case, it must be a system URL as the url() function will generate the alias.
- If you provide '<front>', it generates a link to the site's base URL (again via the url() function).
- If you provide a path, and 'alias' is set to TRUE (see below), it is used as is.
$options An associative array of additional options, with the following keys:
- 'attributes' An associative array of HTML attributes to apply to the anchor tag.
- 'query' A query string to append to the link, or an array of query key/value properties.
- 'fragment' A fragment identifier (named anchor) to append to the link. Do not include the '#' character.
- 'absolute' (default FALSE) Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.
- 'html' (default FALSE) Whether the title is HTML, or just plain-text. For example for making an image a link, this must be set to TRUE, or else you will see the escaped HTML.
- 'alias' (default FALSE) Whether the given path is an alias already.
Return value
an HTML string containing a link to the given path.
Code
includes/common.inc, line 1574
<?php
function l($text, $path, $options = array()) {
global $language;
// Merge in defaults.
$options += array(
'attributes' => array(),
'html' => FALSE,
);
// Append active class.
if (($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) &&
(empty($options['language']) || $options['language']->language == $language->language)) {
if (isset($options['attributes']['class'])) {
$options['attributes']['class'] .= ' active';
}
else {
$options['attributes']['class'] = 'active';
}
}
// Remove all HTML and PHP tags from a tooltip. For best performance, we act only
// if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
$options['attributes']['title'] = strip_tags($options['attributes']['title']);
}
return '<a href="'. check_url(url($path, $options)) .'"'. drupal_attributes($options['attributes']) .'>'. ($options['html'] ? $text : check_plain($text)) .'</a>';
}
?>Login or register to post comments 
the $options array sample
The following link was useful for me so I thought I should post it here:
Anyone up for explaining the $options array in the l() function for D6?
http://groups.drupal.org/node/11554
thanks.
It is not necessary to wrap
It is not necessary to wrap the $text argument with t() as long as the html option is FALSE (the default). l() will automatically check_plain() the link text for you.
check_plain() and t() are different
t() is necessary if you are providing translatable text. The purpose of t() is to provide translation, while check_plain() is used to escape any possible malicious character. Often t() is used an easy way to provide secure text since it pushes things through check_plain, but this is just an added bonus. use t() for translating!.
Example with custom class, attributes
This will produce a link with the class="widelink" and rel="lightbox" attributes on the a tag.
<?phpl(
t('My link'),
'node/56',
array(
'attributes' => array(
'class' => 'widelink',
'rel' => 'lightbox',
)
)
);
?>
Open link a in new window
All components are already mentioned but no example straight into you face, so to open link in new windows use:
l(t('Title'),$url, array('attributes' => array('target' => '_blank')));
Hope this will save time for some of you.
Valid HTML
That's helpful but the HTML won't validate for strict doc types.
Something like:
l(t('Title'), $url, array('attributes' => array('onclick' => 'window.open(this.href); return false;')));should work