You are here

public function QueryBase::tableSort in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/Query/QueryBase.php \Drupal\Core\Entity\Query\QueryBase::tableSort()

Enables sortable tables for this query.

Parameters

$headers: An array of headers of the same structure as described in template_preprocess_table(). Use a 'specifier' in place of a 'field' to specify what to sort on. This can be an entity or a field as described in condition().

Return value

$this The called object.

Overrides QueryInterface::tableSort

File

core/lib/Drupal/Core/Entity/Query/QueryBase.php, line 325

Class

QueryBase
The base entity query class.

Namespace

Drupal\Core\Entity\Query

Code

public function tableSort(&$headers) {

  // If 'field' is not initialized, the header columns aren't clickable.
  foreach ($headers as $key => $header) {
    if (is_array($header) && isset($header['specifier'])) {
      $headers[$key]['field'] = '';
    }
  }
  $order = TableSort::getOrder($headers, \Drupal::request());
  $direction = TableSort::getSort($headers, \Drupal::request());
  foreach ($headers as $header) {
    if (is_array($header) && $header['data'] == $order['name']) {
      $this
        ->sort($header['specifier'], $direction, isset($header['langcode']) ? $header['langcode'] : NULL);
    }
  }
  return $this;
}