tablesort.inc 4.8 KB

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