sort_comments.module in Sort Comments 7
File
sort_comments.module
View source
<?php
define('SORT_COMMENTS_OLDER_FIRST', 1);
define('SORT_COMMENTS_NEWER_FIRST', 2);
function sort_comments_form_node_type_form_alter(&$form, $form_state) {
$form['comment']['comment_default_sorting'] = array(
'#title' => t('Sort'),
'#type' => 'select',
'#options' => array(
1 => t('Older first'),
2 => t('Newer first'),
),
'#default_value' => variable_get('comment_default_sorting_' . $form['#node_type']->type, SORT_COMMENTS_OLDER_FIRST),
);
}
function sort_comments_query_comment_filter_alter(QueryAlterableInterface $query) {
if (($node = $query
->getMetaData('node')) && get_class($query) == 'PagerDefault') {
$sort = variable_get('comment_default_sorting_' . $node->type, SORT_COMMENTS_OLDER_FIRST);
if ($sort == SORT_COMMENTS_NEWER_FIRST) {
$orderby =& $query
->getOrderBy();
$expressions =& $query
->getExpressions();
if (isset($orderby['torder'])) {
unset($expressions['torder']);
unset($orderby['torder']);
$orderby['c.thread'] = 'DESC';
}
else {
$orderby['c.cid'] = 'DESC';
}
}
}
}