You are here

public static function TableSort::getOrder in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Utility/TableSort.php \Drupal\Core\Utility\TableSort::getOrder()

Determines the current sort criterion.

Parameters

array $headers: An array of column headers in the format described in '#type' => 'table'.

\Symfony\Component\HttpFoundation\Request $request: A current request.

Return value

array An associative array describing the criterion, containing the keys:

  • "name": The localized title of the table column.
  • "sql": The name of the database field to sort on.
4 calls to TableSort::getOrder()
QueryBase::tableSort in core/lib/Drupal/Core/Entity/Query/QueryBase.php
Enables sortable tables for this query.
TableSort::getContextFromRequest in core/lib/Drupal/Core/Utility/TableSort.php
Initializes the table sort context.
TableSort::getSort in core/lib/Drupal/Core/Utility/TableSort.php
Determines the current sort direction.
tablesort_get_order in core/includes/tablesort.inc
Determines the current sort criterion.

File

core/lib/Drupal/Core/Utility/TableSort.php, line 108

Class

TableSort
Provides a class for table sorting processing and rendering.

Namespace

Drupal\Core\Utility

Code

public static function getOrder(array $headers, Request $request) {
  $order = $request->query
    ->get('order', '');
  foreach ($headers as $header) {
    if (is_array($header)) {
      if (isset($header['data']) && $order == $header['data']) {
        $default = $header;
        break;
      }
      if (empty($default) && isset($header['sort']) && in_array($header['sort'], [
        self::ASC,
        self::DESC,
      ])) {
        $default = $header;
      }
    }
  }
  if (!isset($default)) {
    $default = reset($headers);
    if (!is_array($default)) {
      $default = [
        'data' => $default,
      ];
    }
  }
  $default += [
    'data' => NULL,
    'field' => NULL,
  ];
  return [
    'name' => $default['data'],
    'sql' => $default['field'],
  ];
}