The Drupal link function is a handy and easy way to create hyperlinks in your module or theme.
The syntax for the link function is:
print l(<em>anchor text</em>, <em>path</em>);
This is an example of a simple link that might exist in a theme template file:
print l('See full post', 'node/'.$node->nid);
The above example would create the following hyper link (assuming that the given $node->nid is 1):
<a href="node/1">See full post</a>
You can easily add attributes to the link.
For example, lets say you want to add a class to your anchor link called "see-more" and
you want to add target="_blank"
Drupal 5:
$attributes = array( 'class' => 'see-more', 'target' => '_blank' ); print l('See full post', 'node/'.$node->nid, $attributes);
Drupal 6:
$options['attributes'] = array( 'class' => 'see-more', 'target' => '_blank' ); print l('See full post', 'node/'.$node->nid, $options);
The above examples would create the following hyper link:
<a href="node/1" class="see-more" target="_blank">See full post</a>
If the link is external, then use the full URL including the http://.
For example:
print l('Google', 'http://www.google.com');
This would create:
<a href="http://www.google.com">Google</a>
It is generally easier to create a hyper link using the link l() function that typing out the full html for an anchor link.
Using the link function is more secure because check_plain and check_url will run automatically, which helps prevent xss attacks.
And finally, active classes are automatically assigned when using the link function.
For more information, see the Drupal API page on the Drupal link function
Does Drupal development make your head explode and drive you crazy?
Why not learn from someone who has paved the way instead?
Sign up to my upcoming learning series.
I am Blair Wadman and this is where I write about Drupal, PHP, CSS etc
© Blair Wadman
2005 - 2011
What I do to call function with a link?
Instead:
print l('See full post', 'node/'.$node->nid, $options);
I want 'mymodule/' not 'node/' but in my site not work!
Thanks
Post new comment