handler_sort.inc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsHandlerSort.
  5. */
  6. /**
  7. * Class for sorting results according to a specified field.
  8. */
  9. class SearchApiViewsHandlerSort extends views_handler_sort {
  10. /**
  11. * The associated views query object.
  12. *
  13. * @var SearchApiViewsQuery
  14. */
  15. public $query;
  16. /**
  17. * Called to add the sort to a query.
  18. */
  19. public function query() {
  20. // When there are exposed sorts, the "exposed form" plugin will set
  21. // $query->orderby to an empty array. Therefore, if that property is set,
  22. // we here remove all previous sorts.
  23. if (isset($this->query->orderby)) {
  24. unset($this->query->orderby);
  25. $sort = &$this->query->getSort();
  26. $sort = array();
  27. unset($sort);
  28. }
  29. // If two of the same fields are used for sort, ignore the latter in order
  30. // for the prior to take precedence. (Temporary workaround until
  31. // https://www.drupal.org/node/2145547 is fixed in Views.)
  32. $alreadySorted = $this->query->getSort();
  33. if (is_array($alreadySorted) && isset($alreadySorted[$this->real_field])) {
  34. return;
  35. }
  36. try {
  37. $this->query->sort($this->real_field, $this->options['order']);
  38. }
  39. catch (SearchApiException $e) {
  40. $this->query->abort($e->getMessage());
  41. }
  42. }
  43. }