When Drupal paginates comments, the link in the comments block is not properly addressed for those not appearing on the first page. For example, the link for a comment on the first page may look like this:

This works fine, but when linking to a comment located on the second page, the link is identical in form. It should, of course, be:

At first I thought it'd be a fairly simple query to pop in to theme_comment_block, but the threaded state of my comments makes it more challenging. Anyone have a solution to this?

Looking through the API, this issue appears to affect all current versions of Drupal.

Comments

meatbites’s picture

Any quick suggestions, perhaps, on how one might figure out how to calculate what page a threaded comment appears on? Very much in need of a fix for this.

skiminki’s picture

This is something we did on Drupal 5, may work on D6 too:

// TODO: This function works only for threaded view with oldest first logic
// First comment gets ordinal 0, second get 1, ...
function us08_get_comment_ordinal($nid, $cid) {
  $ord=
    db_result(db_query("SELECT count(*) FROM comments c, comments d ".
                       "WHERE c.nid=%d AND d.nid=%d AND d.cid=%d and ".
                       "SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1)) < SUBSTRING(d.thread, 1, (LENGTH(d.thread) - 1))",
                       $nid, $nid, $cid));
  return $ord;
}

// page number, starts from 0
function us08_get_comment_page_num($nid, $cid) {
  $ord=us08_get_comment_ordinal($nid, $cid);
  $comments_per_page = variable_get('comment_default_per_page', '50');
  return floor($ord/$comments_per_page);
}

function us08_comment_url($nid, $cid) {
  $comment_page=us08_get_comment_page_num($nid, $cid);
  $query=NULL;
  if ($comment_page) {
    $query='page='.$comment_page;
  }

  return url('node/'.$nid, $query, 'comment-'.$cid);
}

So, us08_get_comment_ordinal() determines the comment order number, beginning from 0. us08_get_comment_page() determines the page of the comment and finally, us08_comment_url() formats url for the comment. This code was derived from http://api.drupal.org/api/function/comment_render/5

damien tournoud’s picture

Status: Active » Closed (duplicate)

This looks like a duplicate of #26966: Fix comment links when paging is used..