You are here

public static function TableSort::header in Drupal 10

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

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 $context: The current table sort context as returned from TableSort::getContextFromRequest() method.

Throws

\Exception

See also

getContextFromRequest()

1 call to TableSort::header()
template_preprocess_table in core/includes/theme.inc
Prepares variables for table templates.

File

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

Class

TableSort
Provides a class for table sorting processing and rendering.

Namespace

Drupal\Core\Utility

Code

public static function header(&$cell_content, array &$cell_attributes, array $header, array $context) {

  // Special formatting for the currently sorted column header.
  if (isset($cell_attributes['field'])) {
    $title = new TranslatableMarkup('sort by @s', [
      '@s' => $cell_content,
    ]);
    if ($cell_content == $context['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'] = $context['sort'] == self::ASC ? 'ascending' : 'descending';
      $context['sort'] = $context['sort'] == self::ASC ? self::DESC : self::ASC;
      $cell_attributes['class'][] = 'is-active';
      $tablesort_indicator = [
        '#theme' => 'tablesort_indicator',
        '#style' => $context['sort'],
      ];
      $image = \Drupal::service('renderer')
        ->render($tablesort_indicator);
    }
    else {

      // This determines the sort order when the column gets first clicked by
      // the user. It is "asc" by default but the sort can be changed if
      // $cell['initial_click_sort'] is defined. The possible values are "asc"
      // or "desc".
      $context['sort'] = $cell_attributes['initial_click_sort'] ?? self::ASC;
      $image = '';
    }
    $cell_content = Link::createFromRoute(new FormattableMarkup('@cell_content@image', [
      '@cell_content' => $cell_content,
      '@image' => $image,
    ]), '<current>', [], [
      'attributes' => [
        'title' => $title,
        'rel' => 'nofollow',
      ],
      'query' => array_merge($context['query'], [
        'sort' => $context['sort'],
        'order' => $cell_content,
      ]),
    ]);
    unset($cell_attributes['field'], $cell_attributes['sort'], $cell_attributes['initial_click_sort']);
  }
}