Description

If you want to show a dept counter of all comments you can use this snippet.
The counters work like this:

  • 1 comment
    • 1 reply
    • 2 reply
      • 1 reply to reply
    • 3 reply
  • 2 comment
  • 3 comment

Step 1 of 2

Add this to your template.php

/**
 * Implementation of hook_preprocess_comment to add the comment_depth_counter to every comment.
 *
 */
function YOUR_THEME_NAME_preprocess_comment(&$variables){
	static $depths_counter;
	$variables['comment_depth_counter'] = ++$depths_counter[$variables['comment']->nid][$variables['comment']->pid];
}

Step 2 of 2

Use the variable $comment_depth_counter in your comment.tpl.php file.

Comments

Adam Neutrik’s picture

I did something similar for numbered node teasers in D7.

/**
 * Override or insert variables into the node template.
 */
function fassette_preprocess_node(&$variables) {

  // Implementation of hook_preprocess_node to add node_depth_counter to every node.
    static $depths_counter;
    $variables['node_depth_counter'] = ++$depths_counter;
}

Step 2: Use the variable $node_depth_counter in your node.tpl.php file.
For example here:
<div id="node-<?php print $node->nid; ?>" class="<?php print $node_depth_counter; ?> <?php print $classes; ?> clearfix"<?php print $attributes; ?>>

If you ask me, this simple task should be included in core generally. We have sooo many class attributes rendered already, why we don't have a simple counted number ot the node when it gets rendered to the output of more than one node in a page? This would make us possible to define css for every third or 10th node in the list (useful to clearfix in a magazine kind of template for example). I am sure this would be much easier and better performed by getting implementented in core than in prerocessing functions.

intrafusion’s picture

Anyone landing here from Google, etc. the Drupal 10 version is:

use Drupal\Component\Utility\Number;

/**
 * Implements hook_preprocess_HOOK().
 */
function YOUR_THEME_NAME_preprocess_comment(&$variables) {
  /** @var \Drupal\comment\CommentInterface $comment */
  $comment = $variables['comment'];
  $thread = rtrim($comment->getThread(), '/');
  $variables['comment_number'] = Number::alphadecimalToInt($thread);
}