8

How should I configure Drupal 8 to sort comments from newest to oldest, similarly to what the Comment Goodness module does in Drupal 7?

  • Find some clue: \Drupal\Core\Entity\EntityViewBuilder::viewMultiple but still didn't know how to order – bluesky_still Jan 6 '17 at 7:07
5

As CommentStorage::loadThread adds comment_filter tag to its query, you may use hook_query_TAG_alter hook to change comments order:

/**
 * Implements hook_query_TAG_alter() for comment_filter tag.
 *
 * @see CommentStorage::loadThread().
 */
function mymodule_query_comment_filter_alter(Drupal\Core\Database\Query\AlterableInterface $query) {
  // Change comment order to DESC for 'comment' field.
  if ($query->getMetaData('field_name') == 'comment') {
    /** @var \Drupal\Core\Database\Query\SelectInterface $query */
    $order_by = &$query->getOrderBy();
    // 'c.cid' is for flat comment lists.
    if (isset($order_by['c.cid']) && $order_by['c.cid'] == 'ASC') {
      $order_by['c.cid'] = 'DESC';
    }
    // 'torder' is for threated comment lists.
    if (isset($order_by['torder']) && $order_by['torder'] == 'ASC') {
      $order_by['torder'] = 'DESC';
    }
  }
}
  • Thank you @Stanislav Agapov I had to modify your code. For me works this one: if ($query->getMetaData('field_name') == 'comment') { /** @var \Drupal\Core\Database\Query\SelectInterface $query */ $order_by = &$query->getOrderBy(); if (isset($order_by['torder']) && $order_by['torder'] == 'ASC') { $order_by['torder'] = 'DESC'; } } – seb Feb 2 '17 at 12:52
  • 1
    @seb your solution works for threaded commens. For flat comment lists works mine. I updated my answer. Thank you! – Stanislav Agapov Feb 3 '17 at 10:51
  • your solution for threaded comments is not going to sort properly, making latest replies visually appear above parent comments. @echo's approach with unsetting toroder and sorting by thread worked – Alex.Designworks Mar 29 '17 at 7:57
4

Assuming your comment field's machine name is field_comments, put this code into a module to get descending comments. I was having trouble with threaded comments, but this works since it alters the query by the c.thread database column instead of just 'torder'

/**
 * Implements hook_query_TAG_alter() for comment_filter tag.
 *
 * @see CommentStorage::loadThread().
 */
function MYMODULE_query_comment_filter_alter(Drupal\Core\Database\Query\AlterableInterface $query) {

  // Change comment order to DESC for 'comment' field.
  if ($query->getMetaData('field_name') == 'field_comments') {

    $order_by = &$query->getOrderBy();
    $expressions = &$query->getExpressions();
    // Sorting for threaded comments.
    if (isset($order_by['torder']) && $order_by['torder'] == 'ASC') {
      // Get rid of the expressions that prepare the threads for ASC ordering.
      unset($expressions['torder']);
      unset($order_by['torder']);
      // Simply order by the thread field.
      $order_by['c.thread'] = 'DESC';
    }

  }

}
  • This is the properly working solution for threaded comments. Thank you – Alex.Designworks Mar 29 '17 at 7:58
3

You could create a comments view and sort them there.

1

For Drupal 8 you can use the next module: Comments order.

Link: https://www.drupal.org/project/comments_order

This module provides to change comments order (sorting functionality) in Drupal 8. You can pick comment order (newest first or oldest first) per node type, editing your comment type field on the "Manage fields" tab (node type administration page).

Not only in flat, but also in threaded display! If you use threaded display, you can pick how will able to sort children comments. Examples below.

0

core/modules/comment/src/CommentStorage.php line 302. I think the only way is hack the core! & only for FLAT module.

    if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
  $query->orderBy('c.cid', 'DESC');
}
else {
  // See comment above. Analysis reveals that this doesn't cost too
  // much. It scales much much better than having the whole comment
  // structure.
  $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
  $query->orderBy('torder', 'ASC');
}
  • There is no need to hack core when an entity definition can be altered. – kiamlaluno Jan 6 '17 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.