You are here

function tablesort_get_order in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/includes/tablesort.inc \tablesort_get_order()

Determines the current sort criterion.

Parameters

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

Return value

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_get_order()
QueryBase::tableSort in core/lib/Drupal/Core/Entity/Query/QueryBase.php
Enables sortable tables for this query.
TableSortExtender::order in core/lib/Drupal/Core/Database/Query/TableSortExtender.php
Determine the current sort criterion.
tablesort_get_sort in core/includes/tablesort.inc
Determines the current sort direction.
tablesort_init in core/includes/tablesort.inc
Initializes the table sort context.

File

core/includes/tablesort.inc, line 97
Functions to aid in the creation of sortable tables.

Code

function tablesort_get_order($headers) {
  $order = \Drupal::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']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
        $default = $header;
      }
    }
  }
  if (!isset($default)) {
    $default = reset($headers);
    if (!is_array($default)) {
      $default = array(
        'data' => $default,
      );
    }
  }
  $default += array(
    'data' => NULL,
    'field' => NULL,
  );
  return array(
    'name' => $default['data'],
    'sql' => $default['field'],
  );
}