diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index fdaeba7..6cc4de5 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -83,6 +83,12 @@ const COMMENT_OPEN = 2; /** + * Comments sortings. + */ +const COMMENT_SORTING_ASC = 'ASC'; +const COMMENT_SORTING_DESC = 'DESC'; + +/** * Denotes that access is denied for an entity to which a comment field is * attached and no {%entity_type}_access function exists. * @@ -738,8 +744,10 @@ function comment_add(EntityInterface $entity, $field_name = 'comment', $pid = NU * The comment display mode; COMMENT_MODE_FLAT or COMMENT_MODE_THREADED. * @param int $comments_per_page * The amount of comments to display per page. + * @param string $sort + * Sorting order value. Could be ASC or DESC. * - * @return + * @return array * An array of the IDs of the comment to be displayed. * * To display threaded comments in the correct order we keep a 'thread' field @@ -796,7 +804,7 @@ function comment_add(EntityInterface $entity, $field_name = 'comment', $pid = NU * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need * to consider the trailing "/" so we use a substring only. */ -function comment_get_thread(EntityInterface $entity, $field_name, $mode, $comments_per_page) { +function comment_get_thread(EntityInterface $entity, $field_name, $mode, $comments_per_page, $sort = COMMENT_SORTING_ASC) { $query = db_select('comment', 'c') ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); $query->addField('c', 'cid'); @@ -828,14 +836,22 @@ function comment_get_thread(EntityInterface $entity, $field_name, $mode, $commen $count_query->condition('c.status', COMMENT_PUBLISHED); } if ($mode == COMMENT_MODE_FLAT) { - $query->orderBy('c.cid', 'ASC'); + $query->orderBy('c.cid', $sort); } 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'); + switch ($sort) { + case COMMENT_SORTING_DESC: + $query->orderBy('c.thread', $sort); + break; + + default: + $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder'); + $query->orderBy('torder', $sort); + break; + } } $query->setCountQuery($count_query); diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/field/field_type/CommentItem.php b/core/modules/comment/lib/Drupal/comment/Plugin/field/field_type/CommentItem.php index 6f9ad12..facb0ec 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/field/field_type/CommentItem.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/field/field_type/CommentItem.php @@ -23,6 +23,7 @@ * instance_settings = { * "default_mode" = COMMENT_MODE_THREADED, * "per_page" = 50, + * "sort" = COMMENT_SORTING_ASC, * "form_location" = COMMENT_FORM_BELOW, * "anonymous" = COMMENT_ANONYMOUS_MAYNOT_CONTACT, * "subject" = 1, @@ -100,6 +101,15 @@ public function instanceSettingsForm(array $form, array &$form_state) { '#default_value' => $settings['default_mode'], '#description' => t('Show comment replies in a threaded list.'), ); + $element['comment']['sort'] = array( + '#type' => 'select', + '#title' => t('Sorting order'), + '#default_value' => $settings['sort'], + '#options' => array( + COMMENT_SORTING_ASC => t('Ascending'), + COMMENT_SORTING_DESC => t('Descending'), + ), + ); $element['comment']['per_page'] = array( '#type' => 'select', '#title' => t('Comments per page'), diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/field/formatter/CommentDefaultFormatter.php b/core/modules/comment/lib/Drupal/comment/Plugin/field/formatter/CommentDefaultFormatter.php index db55970..186d17c 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/field/formatter/CommentDefaultFormatter.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/field/formatter/CommentDefaultFormatter.php @@ -51,7 +51,8 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { // formatter, @see comment_node_update_index(). $mode = $comment_settings['default_mode']; $comments_per_page = $comment_settings['per_page']; - if ($cids = comment_get_thread($entity, $field_name, $mode, $comments_per_page)) { + $sort = $comment_settings['sort']; + if ($cids = comment_get_thread($entity, $field_name, $mode, $comments_per_page, $sort)) { $comments = comment_load_multiple($cids); comment_prepare_thread($comments); $build = comment_view_multiple($comments);