function TableSort::getOrder

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Utility/TableSort.php \Drupal\Core\Utility\TableSort::getOrder()
  2. 8.9.x core/lib/Drupal/Core/Utility/TableSort.php \Drupal\Core\Utility\TableSort::getOrder()
  3. 10 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.
3 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.

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'],
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.