You are here

views_sort_expression.module in Views Sort Expression 8

Same filename and directory in other branches
  1. 7 views_sort_expression.module

File

views_sort_expression.module
View source
<?php

use Drupal\views\ViewExecutable;
use Drupal\views_sort_expression\Plugin\views\sort\ExpressionSort;

/**
 *
 * Implements hook_views_data_alter().
 *
 * Alter the table and field information from hook_views_data().
 *
 * @param array $data
 *   An array of all information about Views tables and fields, collected from
 *   hook_views_data(), passed by reference.
 *
 * @see hook_views_data()
 */
function views_sort_expression_views_data_alter(array &$data) {
  $data['views']['views_sort_expression'] = [
    'title' => t('Expression'),
    'help' => t('Allow you to use an SQL expression.'),
    'sort' => [
      'id' => 'views_sort_expression',
    ],
  ];
}

/**
 * Implements hook_views_post_build().
 *
 * Remove views_sort_expression from group by if it was configured as a group by
 * manually
 *
 * @param \Drupal\views\ViewExecutable $view
 *   The view object about to be processed.
 *
 * @see \Drupal\views\ViewExecutable
 */
function views_sort_expression_views_post_build(ViewExecutable $view) {
  foreach ($view->sort as $handler) {
    if ($handler instanceof ExpressionSort) {
      if ($handler->options['aggregate']) {
        $alias = $handler->realField . '_' . $handler->position;
        $group_by =& $view->build_info['query']
          ->getGroupBy();
        unset($group_by[$alias]);
      }
    }
  }
}