You are here

function oa_comment_views_query_alter in OA Comment 7.2

Implements hook_views_query_alter().

File

./oa_comment.module, line 453

Code

function oa_comment_views_query_alter(&$view, &$query) {

  // If using aggregation, sort handler is ignored.
  // Aggregation is done after views query alter and there is basically no
  // way to customize how it is done, so we add in a tag here to alter the dbtng
  // query. This is very fragile :(
  foreach (array_values($view->sort) as $key => $sort) {
    if ($sort->definition['handler'] == 'views_handler_sort_oa_comment_last_date' && $sort->options['group_type'] != 'group') {
      if (!empty($query->orderby[$key]['field']) && !empty($query->fields[$query->orderby[$key]['field']])) {
        $query->options['query_tags'][] = 'oa_comments_add_last_date_alter';

        // Store the field we are altering to find out later.
        $field_to_change =& drupal_static('oa_comment_query_oa_comments_add_last_date_alter_alter');
        $field_to_change = $sort->relationship . '_' . $sort->real_field;
        break;
      }
    }
  }
  if ($view->name == 'oa_comment_media') {
    $query
      ->add_field('field_oa_related_paragraphs_item_1', 'thread', 'thread');

    // sort by comment thread
    array_unshift($query->orderby, array(
      'field' => 'thread',
      'direction' => 'ASC',
    ));
  }
}