tags:

views:

334

answers:

4

This must be an easy one but I can't find documentation for it online.

I'm trying to use the l() function in Drupal to create a dynamic link. What's the syntax?

At the moment I have:

l('Destination',"path/$user->uid/category")

which points to:

path/%2Fcategory
A: 

Try concatenating the strings instead.
l('Destination',"path/".$user->uid."/category")

as for the documentation, here it is: http://api.drupal.org/api/function/l/4.7

l($text, 
   $path, 
   $attributes = array(), 
   $query = NULL, 
   $fragment = NULL, 
   $absolute = FALSE, 
   $html = FALSE)
Chacha102
Thanks for the suggestion, I'd already tried that, but it gives the same outcome. It seems the l() function is both refusing to deal with the variable and escaping the second slash.
lazysoundsystem
+3  A: 

first of all, if you're working within a function, you'll need to get access to the global user object.

Secondly, if the user is anonymous/not logged in, the $user->uid might not be set or be 0.

lastly to prevent errors, it is common to concatenate variables together with strings


global $user;
if ($user->uid)
{
  l('Destination', 'path/'.$user->uid.'/category')
}
alexanderpas
That was it (global access to the user object). Thanks very much.
lazysoundsystem
+2  A: 

l() is correcting your URL to path/%2Fcategory because it's trying to make a workable link from the string path//category.

Your string is path//category because $user->uid has no value. It has no value because either you haven't pulled up a user object from global $user or user_load(), or your user is anonymous.

I would suggest putting checking the value of $user before calling l(), for example:

global $user; // or $user = user_load($foo);
if ($user) {
l('Destination', 'path/'.$user->uid.'/category');
} else {
l('Destination', 'path/you-are-not-logged-in');
}
anschauung
Yes, that was the problem, and it's good advice. Thanks.
lazysoundsystem
A: 

The documentation for the l() function is located at: http://api.drupal.org/api/function/l/6

Other stuff has been said yet by others :)

redShadow

related questions