How and why to use l() function to create links in Drupal

June 06, 2012
1K
12K


Category:
Tags:

Did you ever create drupal internal or external paths or links ? Which function you used ? First question answer is sure, of course, why not ? . Now come to second questions. You may say i did this by raw html code i.e used html anchor tag, and sometimes used drupals l() function. But I must say, always use drupal's l() function, otherwise you fall in great problems. You may want to know why used drupal l() function to link ? For getting answer, carefully read this tutorilas

Drupal l() function has several benefits

  • To solve clearn url problems
  • For security and safety
  • Easier to read and use and well organized
  • Assign active class to current url; its a great help for themeing..
  • It is recommended by drupal. Drupal allways suggest "all internal links output by modules should be generated by this function if possible".
  • Handles path alias for you ! Convert link to node/nid to Pathauto url !

To solve clean url problems

URLs without question marks, ampersands, or other strange characters are called Clean URLs. For example, http:example.com/foo/bar.

Suppose you want to migrate from another content management system or from static files, the URLs of the content need not change, and unchanging URIs are cool. This can be acheived by drupals l() function.

Suppose your site has url; http:example.com/foo/bar. But Your Web server is not configured to support clean URLs or your host is not configured yet. So when you visit this url; you will get a page not found error or your website redirects you to home page or unexpected errors

And finally, consider my php ini configuration link

<?php print l('PHP ini configuration','blog/php-ini-configuration', array('attributes'=>array('target'=>'_blank'))); ?> 

If clearn URLs is enabled, the above link will be : 'www.webemania.com/blog/php-ini-configuration' and if not enabled link will be 'www.webemania.com/?q=blog/php-ini-configuration'

For security and safety

It Prevents XSS attacks. Cross-site scripting (XSS) is a common form of attack on a web site where the attacker is able to insert his or her own code into a web page, which can then be used for all sorts of mischief. Since l() function runs check_plain and check_urlfunction in the background without you having to touch it, so it is better for security.

Easier to read and use and well organized

The main benefit is that the code is more compact and easier to read than manually created html. For example

<?php
// example 1:  front page
print l(t('Home'), '<front>'); 
// example 2: open front page in new window
print l(t('Home'), '<front>',array('attributes'=>array('target'=>'blank'))); 
?> 

Assign active class to current url

For themers, who wants to add 'active class' for current URL just pass your link to l() function and you are done!

Read what drupal recommended

Drupal allways suggest "all internal links output by modules should be generated by this function if possible"

Handles path alias

Another greatest advantages of l() function is; it handles the aliased paths for you. if you pass "node/23" to a function it would form the the aliased url eg: blog/title-of-post. On top of that it creates the correct path to the page no matter how deep you are on the website.

Some Examples

Example 1: Link to front page

<?php
// example 1:  front page
print l(t('Home'), '<front>'); 
// example 2: open front page in new window
print l(t('Home'), '<front>',array('attributes'=>array('target'=>'blank'))); 
?> 

Example 2: Link to front page and open in new window

<?php
print l(t('Home'), '<front>',array('attributes'=>array('target'=>'blank'))); 
?> 

example 3: how to use html

<?php
print l('<span class="front">Home</span>''<front>',array('html' => TRUE)); 
?> 

example 4: how to use image instead of text link
Lets create my picture path

<?php 
global $base_url;
$path "$base_url/sites/default/files/ceo5.png";
print 
l('<img src="'.$path.'" alt="Webemania CEO Image" />''sites/default/files/ceo5.png', array('html' => TRUE));
?> 

example 5: add class, ids etc

<?php
// add a new class to my php ini configuration path
print l(  t('PHP ini configuration'),  'node/3',  array('attributes' =>array('class'=>'php-ini-conf''id'=>'phpini-conf') ));
?> 

example 6: adding query string to links
Adding a query string to example 5

<?php
// adding query string on example:5
print l(  t('PHP ini configuration'),  'node/33',  array('attributes' =>array('class'=>'php-ini-conf''id'=>'phpini-conf'), 'query'=>array('id'=>'5','status'=>'ok') ));
?> 

example 7: adding destination to query link

<?php
print l(  t('edit'),  'node/'.$nid.'/edit',  array('query' => drupal_get_destination() ));
?> 

example 8: create a hash-only link (to #)

<?php
print l('linktext''', array('fragment' => ' ''external' => TRUE));
?> 

example 9 : specific portions of a page :use fragment attributes
Go to php ini configurations short open tag portion: PHP Short open tag

<?php
l
(  t('PHP ini configuration'),  'node/33',  array('attributes' =>array('class'=>'php-ini-conf''fragment' => "php-tag")));
?> 

CATEGORIES