You are here

function comment_goodness_query_comment_filter_alter in Comment goodness 7

Implements hook_query_TAG_alter().

Alter comments query to order by DESC as well as the default ASC.

The default ASC ordering of threaded comments looks like this where 1 is older than 2.

1 1.1 1.1.1 1.2 2 2.1 2.1.1 2.1.2 2.2

DESC ordering of threaded comments (newest to oldest) should look like this.

2 2.2 2.1 2.1.2 2.1.1 1 1.2 1.1 1.1.1

File

./comment_goodness.module, line 127
Comment goodness provides newest to oldest comment sorting

Code

function comment_goodness_query_comment_filter_alter(QueryAlterableInterface $query) {
  if (($node = $query
    ->getMetaData('node')) && get_class($query) == 'PagerDefault') {

    // Get the configured default sort ordering for this node type.
    $sort = variable_get('comment_default_sorting_' . $node->type, comment_goodness_OLDER_FIRST);

    // The default ordering is ASC (older on top). If the configured sorting is
    // newer on top, change the ordering to DESC.
    if ($sort == comment_goodness_NEWER_FIRST) {
      $orderby =& $query
        ->getOrderBy();
      $expressions =& $query
        ->getExpressions();

      // Sorting for threaded comments.
      if (isset($orderby['torder'])) {

        // Get rid of the expressions that prepare the threads for ASC ordering.
        unset($expressions['torder']);
        unset($orderby['torder']);

        // Simply order by the thread field.
        $orderby['c.thread'] = 'DESC';
      }
      else {
        $direction = 'DESC';
        if (isset($orderby['c.cid'])) {
          unset($orderby['c.cid']);
        }
        $orderby['c.created'] = $direction;
        $orderby['c.cid'] = $direction;
      }
    }
  }
}