TableSortExtender.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\Core\Database\Query;
  3. use Drupal\Core\Database\Connection;
  4. /**
  5. * Query extender class for tablesort queries.
  6. */
  7. class TableSortExtender extends SelectExtender {
  8. /**
  9. * The array of fields that can be sorted by.
  10. */
  11. protected $header = [];
  12. public function __construct(SelectInterface $query, Connection $connection) {
  13. parent::__construct($query, $connection);
  14. // Add convenience tag to mark that this is an extended query. We have to
  15. // do this in the constructor to ensure that it is set before preExecute()
  16. // gets called.
  17. $this->addTag('tablesort');
  18. }
  19. /**
  20. * Order the query based on a header array.
  21. *
  22. * @param array $header
  23. * Table header array.
  24. *
  25. * @return \Drupal\Core\Database\Query\SelectInterface
  26. * The called object.
  27. *
  28. * @see table.html.twig
  29. */
  30. public function orderByHeader(array $header) {
  31. $this->header = $header;
  32. $ts = $this->init();
  33. if (!empty($ts['sql'])) {
  34. // Based on code from db_escape_table(), but this can also contain a dot.
  35. $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
  36. // orderBy() will ensure that only ASC/DESC values are accepted, so we
  37. // don't need to sanitize that here.
  38. $this->orderBy($field, $ts['sort']);
  39. }
  40. return $this;
  41. }
  42. /**
  43. * Initialize the table sort context.
  44. */
  45. protected function init() {
  46. $ts = $this->order();
  47. $ts['sort'] = $this->getSort();
  48. $ts['query'] = $this->getQueryParameters();
  49. return $ts;
  50. }
  51. /**
  52. * Determine the current sort direction.
  53. *
  54. * @return
  55. * The current sort direction ("asc" or "desc").
  56. *
  57. * @see tablesort_get_sort()
  58. */
  59. protected function getSort() {
  60. return tablesort_get_sort($this->header);
  61. }
  62. /**
  63. * Compose a URL query parameter array to append to table sorting requests.
  64. *
  65. * @return
  66. * A URL query parameter array that consists of all components of the current
  67. * page request except for those pertaining to table sorting.
  68. *
  69. * @see tablesort_get_query_parameters()
  70. */
  71. protected function getQueryParameters() {
  72. return tablesort_get_query_parameters();
  73. }
  74. /**
  75. * Determine the current sort criterion.
  76. *
  77. * @return
  78. * An associative array describing the criterion, containing the keys:
  79. * - "name": The localized title of the table column.
  80. * - "sql": The name of the database field to sort on.
  81. *
  82. * @see tablesort_get_order()
  83. */
  84. protected function order() {
  85. return tablesort_get_order($this->header);
  86. }
  87. }