5 * Functions to aid in the creation of sortable tables.
7 * All tables created when rendering a '#type' => 'table' have the option of
8 * having column headers that the user can click on to sort the table by that
12 use Drupal\Component\Render\FormattableMarkup;
14 use Drupal\Component\Utility\UrlHelper;
17 * Initializes the table sort context.
19 function tablesort_init($header) {
20 $ts = tablesort_get_order($header);
21 $ts['sort'] = tablesort_get_sort($header);
22 $ts['query'] = tablesort_get_query_parameters();
27 * Formats a column header.
29 * If the cell in question is the column header for the current sort criterion,
30 * it gets special formatting. All possible sort criteria become links.
32 * @param string $cell_content
33 * The cell content to format. Passed by reference.
34 * @param array $cell_attributes
35 * The cell attributes. Passed by reference.
36 * @param array $header
37 * An array of column headers in the format described in '#type' => 'table'.
39 * The current table sort context as returned from tablesort_init().
41 function tablesort_header(&$cell_content, array &$cell_attributes, array $header, array $ts) {
42 // Special formatting for the currently sorted column header.
43 if (isset($cell_attributes['field'])) {
44 $title = t('sort by @s', ['@s' => $cell_content]);
45 if ($cell_content == $ts['name']) {
46 // aria-sort is a WAI-ARIA property that indicates if items in a table
47 // or grid are sorted in ascending or descending order. See
48 // http://www.w3.org/TR/wai-aria/states_and_properties#aria-sort
49 $cell_attributes['aria-sort'] = ($ts['sort'] == 'asc') ? 'ascending' : 'descending';
50 $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
51 $cell_attributes['class'][] = 'is-active';
52 $tablesort_indicator = [
53 '#theme' => 'tablesort_indicator',
54 '#style' => $ts['sort'],
56 $image = \Drupal::service('renderer')->render($tablesort_indicator);
59 // If the user clicks a different header, we want to sort ascending initially.
63 $cell_content = \Drupal::l(new FormattableMarkup('@cell_content@image', ['@cell_content' => $cell_content, '@image' => $image]), new Url('<current>', [], [
64 'attributes' => ['title' => $title],
65 'query' => array_merge($ts['query'], [
66 'sort' => $ts['sort'],
67 'order' => $cell_content,
71 unset($cell_attributes['field'], $cell_attributes['sort']);
76 * Composes a URL query parameter array for table sorting links.
79 * A URL query parameter array that consists of all components of the current
80 * page request except for those pertaining to table sorting.
82 function tablesort_get_query_parameters() {
83 return UrlHelper::filterQueryParameters(\Drupal::request()->query->all(), ['sort', 'order']);
87 * Determines the current sort criterion.
90 * An array of column headers in the format described in '#type' => 'table'.
93 * An associative array describing the criterion, containing the keys:
94 * - "name": The localized title of the table column.
95 * - "sql": The name of the database field to sort on.
97 function tablesort_get_order($headers) {
98 $order = \Drupal::request()->query->get('order', '');
99 foreach ($headers as $header) {
100 if (is_array($header)) {
101 if (isset($header['data']) && $order == $header['data']) {
106 if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
112 if (!isset($default)) {
113 $default = reset($headers);
114 if (!is_array($default)) {
115 $default = ['data' => $default];
119 $default += ['data' => NULL, 'field' => NULL];
120 return ['name' => $default['data'], 'sql' => $default['field']];
124 * Determines the current sort direction.
127 * An array of column headers in the format described in '#type' => 'table'.
130 * The current sort direction ("asc" or "desc").
132 function tablesort_get_sort($headers) {
133 $query = \Drupal::request()->query;
134 if ($query->has('sort')) {
135 return (strtolower($query->get('sort')) == 'desc') ? 'desc' : 'asc';
137 // The user has not specified a sort. Use the default for the currently sorted
138 // header if specified; otherwise use "asc".
140 // Find out which header is currently being sorted.
141 $ts = tablesort_get_order($headers);
142 foreach ($headers as $header) {
143 if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
144 return $header['sort'];