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

| Published Date: 14 Jun 2012 | Last Updated Date: 04 Sep 2017
Total Reads: 124,923 | Last Visit: 22 Sep 2017 06:53:55 AM

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

  1. <?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

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

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

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

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

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

example 3: how to use html

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

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

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

example 5: add class, ids etc

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

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

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

The output will be: http://webemania.com/blog/php-ini-configuration?id=5&status=ok
example 7: adding destination to query link

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

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

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

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

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



what type of drug is ranitidine order domperidone rabeprazole 20 mg price

how to start hrt mtf domperidone online breastfeeding lack of milk

Как открыть флешку на компьютере?

Post new comment

The content of this field is kept private and will not be shown publicly.
Max length 128 characters
Enter the characters shown in the image.

About This Mania

Hasan Hafiz, Blogger at web mania ltd (www.webemania.com). He has more than 5 years experience in web development platform. He worked as a Sr. Software Developer at Canadian based outsourcing software firm, Lead Developer at countries top most VOIPswitch development company and Sr. Software Engineer at USA based outsourcing software firm . His expertise areas are PHP, MySQL, CSS, Javascript, JQuery, HTML, XML and MVC Framework Codeigniter. He has been along with Drupal from more than 4 years. You may call him Drupal Addicted.