Login

Drupal: sequentially number comments across pages

stplanken | June 6, 2009 - 23:35 | 2 years ago

Drupal

In comment.tpl.php the variable $id contains the comment sequence number, but only for the current page. Each page will start at 1.

In order to properly sequentially number the comments across multiple pages, you need to multiply the number of comments per page by the current page number, plus the comment ID. (The first page is 0.)

Just as I was thinking about this, I found a little gem on the Drupal site. I placed the following in my comment.tpl.php:

<?php
  $page
= $_GET['page'];
  if (!
$page) {
   
$page = 0;
  }
 
$comments_per_page =
   
_comment_get_display_setting('comments_per_page');
 
$comment_index = ( $page * $comments_per_page ) + $id;
  print
l('#' . $comment_index, 'node/' . $comment->nid , array(), null, 'comment-' . $comment->cid, false, true);
?>

The number of comments per page is defined in the comment settings (content/comment/settings) and that value is stored in the variable table as comment_default_per_page. You can retrieve that value with variable_get('comment_default_per_page', 30) or _comment_get_display_setting('comments_per_page'). Then it is simply a matter of multiplying that value with the current page number — retrieved with $_GET['page'] — and offsetting that by the current comment ID $id.

I turned the whole thing into a link with the l() function.

Now the comments are properly numbered across subsequent pages.

Code for Drupal 5.x

A much cleaner solution is to move this code to template.php to keep the comment template clean.

In Drupal 5.x add this to the function _phptemplate_variables() in template.php:

<?php
function _phptemplate_variables($hook, $vars) {

 
//Calculate true comment index
 
if ($hook == 'comment') {

   
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
   
$comments_per_page =
     
_comment_get_display_setting('comments_per_page');
   
$comment_index_nbr = ( $page * $comments_per_page ) + $vars['id'];
   
$vars['comment_index'] = l(
     
'#' . $comment_index_nbr, 'node/' . $vars['comment']->cid,
      array(),
null, 'comment-' . $vars['comment']->cid, false, true
   
);

  }

  return
$vars;
}
?>

You can then add <?php print $comment_index; ?> to the comment.tpl.php template file.

Code for Drupal 6.x and 7.x

Add the following to the function phptemplate_preprocess_comment() in template.php. Please note that the function _comment_get_display_setting() expects an additional parameter in Drupal 6/7.

In Drupal 6 the l() function changed significantly (what a joy changing all nodes that use it).

<?php
function phptemplate_preprocess_comment(&$vars) {

 
// Calculate the true comment index
 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
 
$comments_per_page =
   
_comment_get_display_setting('comments_per_page', $vars['node']);
 
$index = ( $page * $comments_per_page ) + $vars['id'];
 
$vars['comment_index'] = l(
   
'#' . $index, 'node/' . $vars['comment']->nid,
    array(
     
'fragment'=>'comment-' . $vars['comment']->cid,
     
'attributes' => array(
       
'title' => t("Link to this comment")
      ),
    )
  );

  return
$vars;
}
?>

Again, you can use <?php print $comment_index; ?> in the comment template file to display this index number.

#1

|

In D7 this works for me...

<?php
/* Preprocess comments */
function stomper_preprocess_comment(&$vars) {
 
// set a comment id...
 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
 
$comments_per_page = variable_get('comment_default_per_page_' . $vars['node']->type, 50);
 
$vars['index'] = ( $page * $comments_per_page ) + $vars['id'];
  ...
}
?>
#2

|

Your code is pretty much the same, with these differences:

  • Compacter page retrieval code. I like it better.
  • The reliance on the function variable_get() to retrieve the comments per page value. In essence, the function _comment_get_display_setting() calls the same function.
  • Your code lacks the creation of a link to the comment.
<?php
 
// Page number
  $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
  // Retrieve comments per page for current node type
  $comments_per_page = variable_get('comment_default_per_page_' .
 
$vars['node']->type, 1);
  // Calculate true comment index
  $index = $page * $comments_per_page + $vars['id'];
  // Return comment index as a link to the current comment
  $vars['comment_index_test'] = l(
    '#' . $index, 'node/' . $vars['comment']->nid,
    array(
      'fragment'=>'comment-' . $vars['comment']->cid,
      'attributes' => array(
        'title' => t("Permanent link to this comment")
      ),
    )
 );
?>

Good to know it still works in Drupal 7. I haven't taken the plunge yet as there are still several modules without Drupal 7 equivalent.

Thanks for your feedback!

#3

|

Awesome!! Works great. Thanks for sharing.

#4

|

You're welcome!

Post new comment

Image CAPTCHA
Enter the characters shown in the image.

Current weather Sundre

Current Conditions: 2.3°C

Observed at: Sundre Airport 08:00 AM MDT Saturday 19 May 2012
Temperature: 2.3°C
Pressure / Tendency: 102.2 kPa rising
Humidity: 75 %
Dewpoint: -1.7°C
Wind: NW 5 km/h
Air Quality Health Index: N/A