Drupal 5: fix comment permalinks with pagination

having done some more work on making Drupal Froums look nice and work more like a forum, I noticed a potential bug in the way Drupal handles pagination on comments. If you have a node with many comments and you have enabled comment pagination, you will notice:

  • After posting a comment in, you are redirected back to page 1, on a multipaged forum topic this is really annoying.
  • Permalinks to comments do not work because the comment_form_submit does not pass in the query (page number)
  • Changing the comments per page will generate a different permalink for each comment, without creating a dedicated callback and redirecting the user to the specific post (comment/permalink/comment_ID), there doesnt seem to be a fix for this

It is easy enough to redirect the user to (node/NODE_NID?page=PAGE_ID#COMMENT_ID) after a comment gets submitted. Here is what I did:

<?php
function mymodule_form_alter($form_id, &$form) { 
  if (
$form_id == 'forum_node_form' && $form_id == 'comment_form'){
   
$form['#redirect'] = mymodule_comment_redirect($form['nid']['#value']);
  } 
}

function
mymodule_comment_redirect($nid){
  global
$user;
 
$uid = $user->uid;
 
 
//get the last comment ID
 
$sql = "SELECT cid FROM {comments} WHERE nid = %d AND uid = %d ORDER BY timestamp DESC LIMIT 1";
 
$cid = db_result(db_query($sql, $nid, $uid)); 

 
//work out totals etc
 
$sql2 = "SELECT count(cid) as total FROM {comments} WHERE nid = %d";
 
$comment_total = db_result(db_query($sql2, $nid));
 
$comment_default_per_page = variable_get('comment_default_per_page', 10);
 
$last_page = floor($comment_total/$comment_default_per_page);
 
 
//return the redirect url with fragment pager id
 
return array('node/'. $nid, "page=".$last_page, "comment-$cid");
}
?>


Bookmark and Share

3 comments

Anonymous's picture

Luckily Drupal 6 handles pagination much better. :)

What forums are you working on? Is there a link you can share?

Michelle

Janak's picture

If I had a $ for every time I said to myself "I wish this site was in Drupal 6" haha. Sadly the upgrade to the website is not on the cards until next year and I have to make do with D5 for now.

The forums are for onw of the work websites, which is currently on the dev server. But i will send you a link for sure, afterall, without your modules it wouldnt have been as easy :)

Anonymous's picture

Cool, I look forward to seeing it. :)

Michelle

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options