function comment_sort_created_query_comment_filter_alter in Comment sort by created 7
Implements hook_query_TAG_alter().
Alter comments query to join with the comment_sort_created table and sort by the 'created_thread' column instead. The 'created_thread' is just like the 'thread' column in the comment table but using the 'created' timestamp instead of the comment id for sorting.
File
- ./
comment_sort_created.module, line 26 - Sorts comments by creation date.
Code
function comment_sort_created_query_comment_filter_alter(QueryAlterableInterface $query) {
if (get_class($query) == 'PagerDefault' && ($node = $query
->getMetaData('node'))) {
// Should the order of comments for this content type be corrected?
if (!variable_get('comment_sort_created_' . $node->type, FALSE)) {
return;
}
// Get the configured default sort ordering for this node type.
$order = variable_get('comment_sort_created_order_' . $node->type, COMMENT_SORT_CREATED_OLDER_FIRST);
$orderby =& $query
->getOrderBy();
$expressions =& $query
->getExpressions();
// Sorting for threaded comments.
if (isset($orderby['torder'])) {
// Remove standard sorting expressions.
unset($expressions['torder']);
unset($orderby['torder']);
// Join with our table.
$query
->join('comment_sort_created', 'csc', 'c.cid = csc.cid');
// Add expression to order by created_thread.
$query
->addExpression('SUBSTRING(csc.created_thread, 1, (LENGTH(csc.created_thread) - 1))', 'torder');
$query
->orderBy('torder', $order);
}
else {
// To make cid a secondary criterium we have to remove it first.
if (isset($orderby['c.cid'])) {
unset($orderby['c.cid']);
}
// Now first sort aftetr created timestamp, and if timestamps are
// equal, use comment id instead.
$orderby['c.created'] = $order;
$orderby['c.cid'] = $order;
}
}
}