You are here

Creating links in Drupal 7 using url() and l()

Dealing with links in custom code isn't as straight forward as creating a link a link in HTML.  I this article we will show you how to add links to your custom code.

The Basics

$output.='<a href="'./my-new-page.'">My Page</a>';

If the site has clean URLs on the generated, the link will look like:

mysite.com/my-new-page

This general approach can cause problems.  For instance if you disable clean URLs or migrating between development and production servers a 404 page not found error will occur. To fix this we can use url().

$url('/my-new-page')

This will print the following URL: 

With clean URLs enabled: /my-new-page

With clean URLs disabled: ?q=/my-new-page

 

What about using # in the URL

Some times you need to use an anchor for in page navigation  

/my-new-page#section2

As the character # is not escaped this link will generate a 404 page not found error.

So to fix this we need to dig a little deeper in to the url() helper function.  The url() has some options that can help and in this case the option is 'fragment'.  Fragment replaces the # and attaches the named anchor to the fragment.

The url() Options need to be in an array format such as, array('fragment'=>'section2') and when added to the url() would look like 

url('/my-new-page',array('fragment'=>'section2'));

This will print out as: /my-new-page#section2

N.B: the # can also be used to access the admin overlay, array('fragment'=>'overlay=node/1/edit').  This will print out as #overlay=node/1/edit

 

Creating the Link

Ok, now we have the URL sorted out but now we need to make is useful.  We could just add the add the url() to an anchor $output.='<a href="'.url('/my-new-page',array('fragment'=>'section2')).'">My Page</a>';  but this isn’t a good idea and will lead to problems down the track.  This is where l() comes into play.

So l() prints out a link, <a href='the link path'>the link title</a> or in the terms of l()

l('the link title','the link path'); 

N.B: were not going to go into adding CSS classes, just the basic link function.

l('My Page',$url('/my-new-page'));

It is best practice to translate any raw text using the t() function, t('My Page').

Therefore, the code would be:

$output.=l(t('My Page'),$url('/my-new-page')); 

This will print out 

<a href="/my-new-page">My Page</a>

 

If you need to add an anchor to the link 

$output.=l(t('My Page'),$url('/my-new-page',array('fragment'=>'overlay=node/1/edit'))); 

which will pring out, <a href="/my-new-page#overlay=node/1/edit">My Page</a>

 

if you need to call the same page with an anchor or you dont want the user to navigate away from the current page do the following

$output.=l(t('My Page'),$url($baseurl,array('fragment'=>'overlay=node/1/edit'))); 

 

if the baseurl isn’t set then add ‘absolute' to the array 

$output.=l(t('My Page'),$url($baseurl,array(‘absolute’=>TRUE,'fragment'=>'overlay=node/1/edit')));

This will ensure the domain name is in the link.

 

What about puting links in theme tables?

Some times you need to add link to table using the theme layer.  Links in the theme layer neeed to be added as arrays and the array key needs to be 'data' 

The structure is a little different than l(), but basically your adding a string of data in to a theme table cell.  The string is in the form of an array and the arrays 'type' needs to identify that the array is a link.

The array needs the following: 

A type

array('data' =>
    array('#type' => 'link', '#title' => t('Edit Node'),
    '#href' => url($base_url,array('absolute'=>TRUE,'fragment'=>'overlay=node/1'/edit'))))

 

A title

array('data' => array('#type' => 'link', '#title' => t('Edit Node'), '#href' => url($base_url,array('absolute'=>TRUE,'fragment'=>'overlay=node/1'/edit'))))

 

A href

array('data' => array('#type' => 'link', '#title' => t('Edit Node'), '#href' =>url($base_url,array('absolute'=>TRUE,'fragment'=>'overlay=node/1'/edit'))))

 

N.B: dont forget you can add any number of links to the array such as Edit and Delete.