Mercurial > hg > cmmr2012-drupal-site
comparison core/includes/tablesort.inc @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
7 * All tables created when rendering a '#type' => 'table' have the option of | 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 | 8 * having column headers that the user can click on to sort the table by that |
9 * column. | 9 * column. |
10 */ | 10 */ |
11 | 11 |
12 use Drupal\Component\Render\FormattableMarkup; | 12 use Drupal\Core\Utility\TableSort; |
13 use Drupal\Core\Url; | |
14 use Drupal\Component\Utility\UrlHelper; | |
15 | 13 |
16 /** | 14 /** |
17 * Initializes the table sort context. | 15 * Initializes the table sort context. |
16 * | |
17 * @deprecated as of Drupal 8.7.x and will be removed before Drupal 9.0.0. Use | |
18 * \Drupal\Core\Utility\TableSort::getContextFromRequest() instead. | |
19 * | |
20 * @see \Drupal\Core\Utility\TableSortInterface::getContextFromRequest() | |
21 * @see https://www.drupal.org/node/3009182 | |
18 */ | 22 */ |
19 function tablesort_init($header) { | 23 function tablesort_init($header) { |
20 $ts = tablesort_get_order($header); | 24 @trigger_error(__FUNCTION__ . '() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Utility\TableSort::getContextFromRequest() instead. See https://www.drupal.org/node/3009182', E_USER_DEPRECATED); |
21 $ts['sort'] = tablesort_get_sort($header); | 25 return TableSort::getContextFromRequest($header, \Drupal::request()); |
22 $ts['query'] = tablesort_get_query_parameters(); | |
23 return $ts; | |
24 } | 26 } |
25 | 27 |
26 /** | 28 /** |
27 * Formats a column header. | 29 * Formats a column header. |
28 * | 30 * |
35 * The cell attributes. Passed by reference. | 37 * The cell attributes. Passed by reference. |
36 * @param array $header | 38 * @param array $header |
37 * An array of column headers in the format described in '#type' => 'table'. | 39 * An array of column headers in the format described in '#type' => 'table'. |
38 * @param array $ts | 40 * @param array $ts |
39 * The current table sort context as returned from tablesort_init(). | 41 * The current table sort context as returned from tablesort_init(). |
42 * | |
43 * @deprecated as of Drupal 8.7.x and will be removed before Drupal 9.0.0. Use | |
44 * \Drupal\Core\Utility\TableSort::header() instead. | |
45 * | |
46 * @see \Drupal\Core\Utility\TableSortInterface::header() | |
47 * @see https://www.drupal.org/node/3009182 | |
40 */ | 48 */ |
41 function tablesort_header(&$cell_content, array &$cell_attributes, array $header, array $ts) { | 49 function tablesort_header(&$cell_content, array &$cell_attributes, array $header, array $ts) { |
42 // Special formatting for the currently sorted column header. | 50 @trigger_error(__FUNCTION__ . '() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Utility\TableSort::header() instead. See https://www.drupal.org/node/3009182', E_USER_DEPRECATED); |
43 if (isset($cell_attributes['field'])) { | 51 TableSort::header($cell_content, $cell_attributes, $header, $ts); |
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'], | |
55 ]; | |
56 $image = \Drupal::service('renderer')->render($tablesort_indicator); | |
57 } | |
58 else { | |
59 // If the user clicks a different header, we want to sort ascending initially. | |
60 $ts['sort'] = 'asc'; | |
61 $image = ''; | |
62 } | |
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, | |
68 ]), | |
69 ])); | |
70 | |
71 unset($cell_attributes['field'], $cell_attributes['sort']); | |
72 } | |
73 } | 52 } |
74 | 53 |
75 /** | 54 /** |
76 * Composes a URL query parameter array for table sorting links. | 55 * Composes a URL query parameter array for table sorting links. |
77 * | 56 * |
78 * @return | 57 * @return |
79 * A URL query parameter array that consists of all components of the current | 58 * A URL query parameter array that consists of all components of the current |
80 * page request except for those pertaining to table sorting. | 59 * page request except for those pertaining to table sorting. |
60 * | |
61 * @deprecated as of Drupal 8.7.x and will be removed before Drupal 9.0.0. Use | |
62 * \Drupal\Core\Utility\TableSort::getQueryParameters() instead. | |
63 * | |
64 * @see \Drupal\Core\Utility\TableSort::getQueryParameters() | |
65 * @see https://www.drupal.org/node/3009182 | |
81 */ | 66 */ |
82 function tablesort_get_query_parameters() { | 67 function tablesort_get_query_parameters() { |
83 return UrlHelper::filterQueryParameters(\Drupal::request()->query->all(), ['sort', 'order']); | 68 @trigger_error(__FUNCTION__ . '() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Utility\TableSort::getQueryParameters() instead. See https://www.drupal.org/node/3009182', E_USER_DEPRECATED); |
69 return TableSort::getQueryParameters(\Drupal::request()); | |
84 } | 70 } |
85 | 71 |
86 /** | 72 /** |
87 * Determines the current sort criterion. | 73 * Determines the current sort criterion. |
88 * | 74 * |
91 * | 77 * |
92 * @return | 78 * @return |
93 * An associative array describing the criterion, containing the keys: | 79 * An associative array describing the criterion, containing the keys: |
94 * - "name": The localized title of the table column. | 80 * - "name": The localized title of the table column. |
95 * - "sql": The name of the database field to sort on. | 81 * - "sql": The name of the database field to sort on. |
82 * | |
83 * @deprecated as of Drupal 8.7.x and will be removed before Drupal 9.0.0. Use | |
84 * \Drupal\Core\Utility\TableSort::getOrder() instead. | |
85 * | |
86 * @see \Drupal\Core\Utility\TableSortInterface::getOrder() | |
87 * @see https://www.drupal.org/node/3009182 | |
96 */ | 88 */ |
97 function tablesort_get_order($headers) { | 89 function tablesort_get_order($headers) { |
98 $order = \Drupal::request()->query->get('order', ''); | 90 @trigger_error(__FUNCTION__ . '() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Utility\TableSort::getOrder() instead. See https://www.drupal.org/node/3009182', E_USER_DEPRECATED); |
99 foreach ($headers as $header) { | 91 return TableSort::getOrder($headers, \Drupal::request()); |
100 if (is_array($header)) { | |
101 if (isset($header['data']) && $order == $header['data']) { | |
102 $default = $header; | |
103 break; | |
104 } | |
105 | |
106 if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) { | |
107 $default = $header; | |
108 } | |
109 } | |
110 } | |
111 | |
112 if (!isset($default)) { | |
113 $default = reset($headers); | |
114 if (!is_array($default)) { | |
115 $default = ['data' => $default]; | |
116 } | |
117 } | |
118 | |
119 $default += ['data' => NULL, 'field' => NULL]; | |
120 return ['name' => $default['data'], 'sql' => $default['field']]; | |
121 } | 92 } |
122 | 93 |
123 /** | 94 /** |
124 * Determines the current sort direction. | 95 * Determines the current sort direction. |
125 * | 96 * |
126 * @param $headers | 97 * @param $headers |
127 * An array of column headers in the format described in '#type' => 'table'. | 98 * An array of column headers in the format described in '#type' => 'table'. |
128 * | 99 * |
129 * @return | 100 * @return |
130 * The current sort direction ("asc" or "desc"). | 101 * The current sort direction ("asc" or "desc"). |
102 * | |
103 * @deprecated as of Drupal 8.7.x and will be removed before Drupal 9.0.0. Use | |
104 * \Drupal\Core\Utility\TableSort::getSort() instead. | |
105 * | |
106 * @see \Drupal\Core\Utility\TableSortInterface::getSort() | |
107 * @see https://www.drupal.org/node/3009182 | |
131 */ | 108 */ |
132 function tablesort_get_sort($headers) { | 109 function tablesort_get_sort($headers) { |
133 $query = \Drupal::request()->query; | 110 @trigger_error(__FUNCTION__ . '() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Utility\TableSort::getSort() instead. See https://www.drupal.org/node/3009182', E_USER_DEPRECATED); |
134 if ($query->has('sort')) { | 111 return TableSort::getSort($headers, \Drupal::request()); |
135 return (strtolower($query->get('sort')) == 'desc') ? 'desc' : 'asc'; | |
136 } | |
137 // The user has not specified a sort. Use the default for the currently sorted | |
138 // header if specified; otherwise use "asc". | |
139 else { | |
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']; | |
145 } | |
146 } | |
147 } | |
148 return 'asc'; | |
149 } | 112 } |