Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Functions to aid in the creation of sortable tables.
|
Chris@0
|
6 *
|
Chris@0
|
7 * All tables created when rendering a '#type' => 'table' have the option of
|
Chris@0
|
8 * having column headers that the user can click on to sort the table by that
|
Chris@0
|
9 * column.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
13 use Drupal\Core\Url;
|
Chris@0
|
14 use Drupal\Component\Utility\UrlHelper;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Initializes the table sort context.
|
Chris@0
|
18 */
|
Chris@0
|
19 function tablesort_init($header) {
|
Chris@0
|
20 $ts = tablesort_get_order($header);
|
Chris@0
|
21 $ts['sort'] = tablesort_get_sort($header);
|
Chris@0
|
22 $ts['query'] = tablesort_get_query_parameters();
|
Chris@0
|
23 return $ts;
|
Chris@0
|
24 }
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Formats a column header.
|
Chris@0
|
28 *
|
Chris@0
|
29 * If the cell in question is the column header for the current sort criterion,
|
Chris@0
|
30 * it gets special formatting. All possible sort criteria become links.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @param string $cell_content
|
Chris@0
|
33 * The cell content to format. Passed by reference.
|
Chris@0
|
34 * @param array $cell_attributes
|
Chris@0
|
35 * The cell attributes. Passed by reference.
|
Chris@0
|
36 * @param array $header
|
Chris@0
|
37 * An array of column headers in the format described in '#type' => 'table'.
|
Chris@0
|
38 * @param array $ts
|
Chris@0
|
39 * The current table sort context as returned from tablesort_init().
|
Chris@0
|
40 */
|
Chris@0
|
41 function tablesort_header(&$cell_content, array &$cell_attributes, array $header, array $ts) {
|
Chris@0
|
42 // Special formatting for the currently sorted column header.
|
Chris@0
|
43 if (isset($cell_attributes['field'])) {
|
Chris@0
|
44 $title = t('sort by @s', ['@s' => $cell_content]);
|
Chris@0
|
45 if ($cell_content == $ts['name']) {
|
Chris@0
|
46 // aria-sort is a WAI-ARIA property that indicates if items in a table
|
Chris@0
|
47 // or grid are sorted in ascending or descending order. See
|
Chris@0
|
48 // http://www.w3.org/TR/wai-aria/states_and_properties#aria-sort
|
Chris@0
|
49 $cell_attributes['aria-sort'] = ($ts['sort'] == 'asc') ? 'ascending' : 'descending';
|
Chris@0
|
50 $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
|
Chris@0
|
51 $cell_attributes['class'][] = 'is-active';
|
Chris@0
|
52 $tablesort_indicator = [
|
Chris@0
|
53 '#theme' => 'tablesort_indicator',
|
Chris@0
|
54 '#style' => $ts['sort'],
|
Chris@0
|
55 ];
|
Chris@0
|
56 $image = \Drupal::service('renderer')->render($tablesort_indicator);
|
Chris@0
|
57 }
|
Chris@0
|
58 else {
|
Chris@0
|
59 // If the user clicks a different header, we want to sort ascending initially.
|
Chris@0
|
60 $ts['sort'] = 'asc';
|
Chris@0
|
61 $image = '';
|
Chris@0
|
62 }
|
Chris@0
|
63 $cell_content = \Drupal::l(SafeMarkup::format('@cell_content@image', ['@cell_content' => $cell_content, '@image' => $image]), new Url('<current>', [], [
|
Chris@0
|
64 'attributes' => ['title' => $title],
|
Chris@0
|
65 'query' => array_merge($ts['query'], [
|
Chris@0
|
66 'sort' => $ts['sort'],
|
Chris@0
|
67 'order' => $cell_content,
|
Chris@0
|
68 ]),
|
Chris@0
|
69 ]));
|
Chris@0
|
70
|
Chris@0
|
71 unset($cell_attributes['field'], $cell_attributes['sort']);
|
Chris@0
|
72 }
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * Composes a URL query parameter array for table sorting links.
|
Chris@0
|
77 *
|
Chris@0
|
78 * @return
|
Chris@0
|
79 * A URL query parameter array that consists of all components of the current
|
Chris@0
|
80 * page request except for those pertaining to table sorting.
|
Chris@0
|
81 */
|
Chris@0
|
82 function tablesort_get_query_parameters() {
|
Chris@0
|
83 return UrlHelper::filterQueryParameters(\Drupal::request()->query->all(), ['sort', 'order']);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Determines the current sort criterion.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @param $headers
|
Chris@0
|
90 * An array of column headers in the format described in '#type' => 'table'.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @return
|
Chris@0
|
93 * An associative array describing the criterion, containing the keys:
|
Chris@0
|
94 * - "name": The localized title of the table column.
|
Chris@0
|
95 * - "sql": The name of the database field to sort on.
|
Chris@0
|
96 */
|
Chris@0
|
97 function tablesort_get_order($headers) {
|
Chris@0
|
98 $order = \Drupal::request()->query->get('order', '');
|
Chris@0
|
99 foreach ($headers as $header) {
|
Chris@0
|
100 if (is_array($header)) {
|
Chris@0
|
101 if (isset($header['data']) && $order == $header['data']) {
|
Chris@0
|
102 $default = $header;
|
Chris@0
|
103 break;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
|
Chris@0
|
107 $default = $header;
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 if (!isset($default)) {
|
Chris@0
|
113 $default = reset($headers);
|
Chris@0
|
114 if (!is_array($default)) {
|
Chris@0
|
115 $default = ['data' => $default];
|
Chris@0
|
116 }
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 $default += ['data' => NULL, 'field' => NULL];
|
Chris@0
|
120 return ['name' => $default['data'], 'sql' => $default['field']];
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * Determines the current sort direction.
|
Chris@0
|
125 *
|
Chris@0
|
126 * @param $headers
|
Chris@0
|
127 * An array of column headers in the format described in '#type' => 'table'.
|
Chris@0
|
128 *
|
Chris@0
|
129 * @return
|
Chris@0
|
130 * The current sort direction ("asc" or "desc").
|
Chris@0
|
131 */
|
Chris@0
|
132 function tablesort_get_sort($headers) {
|
Chris@0
|
133 $query = \Drupal::request()->query;
|
Chris@0
|
134 if ($query->has('sort')) {
|
Chris@0
|
135 return (strtolower($query->get('sort')) == 'desc') ? 'desc' : 'asc';
|
Chris@0
|
136 }
|
Chris@0
|
137 // The user has not specified a sort. Use the default for the currently sorted
|
Chris@0
|
138 // header if specified; otherwise use "asc".
|
Chris@0
|
139 else {
|
Chris@0
|
140 // Find out which header is currently being sorted.
|
Chris@0
|
141 $ts = tablesort_get_order($headers);
|
Chris@0
|
142 foreach ($headers as $header) {
|
Chris@0
|
143 if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
|
Chris@0
|
144 return $header['sort'];
|
Chris@0
|
145 }
|
Chris@0
|
146 }
|
Chris@0
|
147 }
|
Chris@0
|
148 return 'asc';
|
Chris@0
|
149 }
|