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

If you have been doing web development for any significant amount of time chances are you know how to build a link by hand using the ‘a href=”link/to/some/page”‘ syntax. That method works fine and can even be used inside of Drupal modules, but it is generally frowned upon by the Drupal community. Using the l() function is the Drupal way of building links programmatically, and though it might take some getting used to, in the end you will find that it saves time.

l() is a function that is available anywhere within a Drupal installation. It accepts three arguments:

  1. $text: The text that will be visible to users for your link
  2. $path: The URL that the link will go to when clicked.
  3. $options: An associative array which allows you to set additional parameters to further customize the link with CSS classes etc… It is here that you would need to establish that the link will be an image if you so desire (I’ll go into this later). The $options array is optional.

l($text, $path, $options = array())

 

In its simplest form you can create a link like so:

print l(‘This is the link text’, ‘node/2’);

 

Of course you shouldn’t really write your links like this, because if you do you link will not be translatable. Instead you should write it like so:

print l(t(‘This is the link text’), ‘node/2’);

I will go into the t() function in a later tutorial.

I don’t know if you can see how this cleans thing up, but it really does make it easier to assemble links dynamically. Another added benefit is that links that are built using the l() function get tied into the main Drupal system and therefore are assigned active tags without you having to know anything about it. Very useful.

How to create an image based link using the l() function for Drupal

The $text section of the l() function accepts any text so this text could be html and therefore could be an image. However by default it filters out html unless you tell it otherwise. To tell the l() function to accept html for its text you need to build the $options array and pass it as an argument.

$options = array(
‘attributes’ => array(),
‘html’ => TRUE,
);
l(”, ‘node/1’, $options);

The ‘html’ =>TRUE, is the key here. Yes, I know, for building an image based link this is a little more than an ideal amount of code but this is the current state of affairs. I personally would rather have 4 arguments with $html = FALSE being the default for the third argument and $options for the 4th. This was changed in Drupal 5 where all of the options were individual arguments, and honestly the Drupal 6 version is better.

Benefits of building links with l() in Drupal programming

The main benefit is that the code is more compact and easier to read than manually created html. Also there is the theming benefit with the automated active classes assigned. Additionally the l() function runs check_plain and check_url in the background without you having to touch it, so it is better for security (Prevents xss attacks).

If you can find more information about the l() function by visiting its Drupal api page at http://api.drupal.org/api/function/l