7.x comment.module comment_get_thread($node, $mode, $comments_per_page)

Retrieve comments for a thread.


$node: The node whose comment(s) needs rendering.

$mode: The comment display mode; COMMENT_MODE_FLAT or COMMENT_MODE_THREADED.

$comments_per_page: The amount of comments to display per page.

To display threaded comments in the correct order we keep a 'thread' field and order by that value. This field keeps this data in a way which is easy to update and convenient to use.

A "thread" value starts at "1". If we add a child (A) to this comment, we assign it a "thread" = "1.1". A child of (A) will have "1.1.1". Next brother of (A) will get "1.2". Next brother of the parent of (A) will get "2" and so on.

First of all note that the thread field stores the depth of the comment: depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc.

Now to get the ordering right, consider this example:

1 1.1 1.1.1 1.2 2

If we "ORDER BY thread ASC" we get the above result, and this is the natural order sorted by time. However, if we "ORDER BY thread DESC" we get:

2 1.2 1.1.1 1.1 1

Clearly, this is not a natural way to see a thread, and users will get confused. The natural order to show a thread by time desc would be:

2 1 1.2 1.1 1.1.1

which is what we already did before the standard pager patch. To achieve this we simply add a "/" at the end of each "thread" value. This way, the thread fields will look like this:

1/ 1.1/ 1.1.1/ 1.2/ 2/

we add "/" since this char is, in ASCII, higher than every number, so if now we "ORDER BY thread DESC" we get the correct order. However this would spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need to consider the trailing "/" so we use a substring only.


modules/comment/comment.module, line 822
Enables users to comment on published content.


function comment_get_thread($node, $mode, $comments_per_page) {
  $query = db_select('comment', 'c')
    ->addField('c', 'cid');
    ->condition('c.nid', $node->nid)
    ->addMetaData('node', $node)
  $count_query = db_select('comment', 'c');
    ->condition('c.nid', $node->nid)
    ->addMetaData('node', $node);
  if (!user_access('administer comments')) {
      ->condition('c.status', COMMENT_PUBLISHED);
      ->condition('c.status', COMMENT_PUBLISHED);
  if ($mode === COMMENT_MODE_FLAT) {
      ->orderBy('c.cid', 'ASC');
  else {

    // See comment above. Analysis reveals that this doesn't cost too
    // much. It scales much much better than having the whole comment
    // structure.
      ->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
      ->orderBy('torder', 'ASC');
  $cids = $query
  return $cids;


kth’s picture

Can somebody explain to my why this function loads a pager as the first thing?
When calling this function other pagers breakes - why is that?

khu’s picture

Ran into trouble with the pagers of a views content pane while calling this function in a views php field.

Replaced with this EFQ which returns a flat array of comment ids:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'comment')
->propertyCondition('nid', $data->nid)
->addMetaData('account', user_load(1));

$result = $query->execute();
$cids = array();
if (isset($result['comment']))
$cids = array_keys($result['comment']);