W3cubDocs

/Drupal 8

function tablesort_header

tablesort_header(&$cell_content, array &$cell_attributes, array $header, array $ts)

Formats a column header.

If the cell in question is the column header for the current sort criterion, it gets special formatting. All possible sort criteria become links.

Parameters

string $cell_content: The cell content to format. Passed by reference.

array $cell_attributes: The cell attributes. Passed by reference.

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

array $ts: The current table sort context as returned from tablesort_init().

File

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

Code

function tablesort_header(&$cell_content, array &$cell_attributes, array $header, array $ts) {
  // Special formatting for the currently sorted column header.
  if (isset($cell_attributes['field'])) {
    $title = t('sort by @s', array('@s' => $cell_content));
    if ($cell_content == $ts['name']) {
      // aria-sort is a WAI-ARIA property that indicates if items in a table
      // or grid are sorted in ascending or descending order. See
      // http://www.w3.org/TR/wai-aria/states_and_properties#aria-sort
      $cell_attributes['aria-sort'] = ($ts['sort'] == 'asc') ? 'ascending' : 'descending';
      $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
      $cell_attributes['class'][] = 'is-active';
      $tablesort_indicator = array(
        '#theme' => 'tablesort_indicator',
        '#style' => $ts['sort'],
      );
      $image = drupal_render($tablesort_indicator);
    }
    else {
      // If the user clicks a different header, we want to sort ascending initially.
      $ts['sort'] = 'asc';
      $image = '';
    }
    $cell_content = \Drupal::l(SafeMarkup::format('@[email protected]', array('@cell_content' => $cell_content, '@image' => $image)), new Url('<current>', [], [
      'attributes' => array('title' => $title),
      'query' => array_merge($ts['query'], array(
        'sort' => $ts['sort'],
        'order' => $cell_content,
      )),
    ]));

    unset($cell_attributes['field'], $cell_attributes['sort']);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!includes!tablesort.inc/function/tablesort_header/8.1.x