I need to build and print a deeplink to any given comment. So that the user can directly access the specific comment with just clicking a link. I could not find a native drupal function to get this so i build it my own.
My solution
<?php
global $base_url;
$base = drupal_lookup_path('alias',"node/".$node->nid);
$path = $base_url.'/'.$base.'#comment-'.$comment->cid;
$link_options = array('html'=> $html);
$commentlink = l($date, $path, $link_options);
?>
To print the link you only have to call <?php print $commentlink;?>
. But i'm pretty sure there is better and much more drupal like way to solve the problem.
The Better Way
Mikeker did it :) As he suggested here are the solution.
<?php
$commentlink = l(
$date,
"node/$comment->nid",
array("fragment" => "comment-$comment->cid"));
?>
Note the little difference bettween Mikeker and my version. array("fragment" => "comment-$comment->cid"));
and array("query" => "comment-$comment->cid"));
The query param will add an ?
to the url. So your path looks like
//…query
http://example.com/path/to/node?comment-2
In opposite to my solution (fragment):
//…fragment
http://example.com/path/to/node#comment-2
Note: Do not include the leading '#' character to a fragment identifier. It will be added by drupal.