Default Search API Sorts Per View in Drupal 7

It's been a while since I've written a post here (especially, Drupal-related). But today I have something interesting to share.

There's a module called Search API sorts (https://drupal.org/project/search_api_sorts">https://drupal.org/project/search_api_sorts) that provides custom sorts and a global sort block for Search API. The module itself is ok, but has one big flaw: all settings are global and will get applied to every view you want to have the sort on. But what if you want to have different default sorts for different views? For example, View 1 to be sorted by created date and View 2 by radioactivity field value? Well, as you may have guessed, regular sitebuilder won't do it via UI, you need to dig deep into code so I'll guide you through then.

If we'll look at the code of search_api_sorts.module we'll see a function that's responsible for applying sorts on the current query, it's called search_api_sorts_search_api_query_alter. The function itself, implements hook_search_api_query_alter() hook, which means, we can easily re-implement that function in our custom module with a few tweaks to reach our goal.

So what we actually need is to create a custom module that'll have less weight than search_api_module (-1) or have same weight but it's name should start on a letter that comes before S. We need this to make sure, that the hook we defined in our custom module kicks in earlier than the hook defined in search_api_sorts.

So we just take search_api_sorts_search_api_query_alter, copy it and put into our custom example_module renaming it accordingly: 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  1. <?php
  2. /**
  3. * Implements hook_search_api_query_alter().
  4. */
  5. function example_module_search_api_query_alter(SearchApiQueryInterface $query) {
  6. if (!user_access('use search_api_sorts')) {
  7. return;
  8. }
  9. // There's already an existing sort, so abort!
  10. $existing = $query->getSort();
  11. if (!empty($existing)) {
  12. return;
  13. }
  14. $search_sorts = search_api_sorts_search_sorts($query->getIndex()->machine_name);
  15. if (empty($search_sorts)) {
  16. return;
  17. }
  18. $default_sort = _search_api_sorts_get_default_sort($search_sorts, $query->getKeys());
  19. // alter sort field and sort order
  20. $sort = $default_sort->field;
  21. $params = drupal_get_query_parameters($_GET, array(
  22. 'q',
  23. 'page'
  24. ));
  25. if (isset($params['sort']) && !empty($params['sort'])) {
  26. $sort = $params['sort'];
  27. }
  28. $order = $default_sort->default_order;
  29. if (isset($params['order']) && !empty($params['order'])) {
  30. $order = $params['order'];
  31. }
  32. if (!empty($order) && !empty($sort)) {
  33. $query->sort($sort, $order);
  34. }
  35. // Static save current search query
  36. $_query = &drupal_static(__FUNCTION__, array());
  37. $_query = $query;
  38. }

Now, we need to modify it a bit to suit our needs:

1) We need to check if the current view is the view we want to put custom sort on, like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  1. <?php
  2. /**
  3. * Implements hook_search_api_query_alter().
  4. */
  5. function example_module_search_api_query_alter(SearchApiQueryInterface $query) {
  6. if (!user_access('use search_api_sorts')) {
  7. return;
  8. }
  9. $view = views_get_current_view();
  10. if (empty($view) || $view->name != 'articles_list') {
  11. return;
  12. }
  13. ...
  14. }

2) We need to tweak $search_sorts variables and set our field (field_radioactivity) as a default sort, like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
  1. foreach ($search_sorts as $key => $sort) {
  2. // We set our desired field as a default sort
  3. if ($sort->field == 'field_shared_radioactivity') {
  4. $search_sorts[$key]->default_sort = 1;
  5. $search_sorts[$key]->default_sort_no_terms = 1;
  6. $search_sorts[$key]->active = TRUE;
  7. $search_sorts[$key]->default_order = 'DESC';
  8. }
  9. // We reset default sorts for other fields
  10. else {
  11. $search_sorts[$key]->default_sort = 0;
  12. $search_sorts[$key]->default_sort_no_terms = 0;
  13. $search_sorts[$key]->active = FALSE;
  14. }
  15. }

So in the end, our modified example_module_search_api_query_alter would look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  1. <?php
  2. /**
  3. * Implements hook_search_api_query_alter().
  4. */
  5. function example_module_search_api_query_alter(SearchApiQueryInterface $query) {
  6. if (!user_access('use search_api_sorts')) {
  7. return;
  8. }
  9. $view = views_get_current_view();
  10. if (empty($view) || $view->name != 'articles_list') {
  11. return;
  12. }
  13. if (!empty($_GET['sort'])) {
  14. return;
  15. }
  16. // There's already an existing sort, so abort!
  17. $existing = $query->getSort();
  18. if (!empty($existing)) {
  19. return;
  20. }
  21. $search_sorts = search_api_sorts_search_sorts($query->getIndex()->machine_name);
  22. foreach ($search_sorts as $key => $sort) {
  23. if ($sort->field == 'field_shared_radioactivity') {
  24. $search_sorts[$key]->default_sort = 1;
  25. $search_sorts[$key]->default_sort_no_terms = 1;
  26. $search_sorts[$key]->active = TRUE;
  27. $search_sorts[$key]->default_order = 'DESC';
  28. }
  29. else {
  30. $search_sorts[$key]->default_sort = 0;
  31. $search_sorts[$key]->default_sort_no_terms = 0;
  32. $search_sorts[$key]->active = FALSE;
  33. }
  34. }
  35. if (empty($search_sorts)) {
  36. return;
  37. }
  38. $default_sort = _search_api_sorts_get_default_sort($search_sorts, $query->getKeys());
  39. // alter sort field and sort order
  40. $sort = $default_sort->field;
  41. $params = drupal_get_query_parameters($_GET, array(
  42. 'q',
  43. 'page'
  44. ));
  45. if (isset($params['sort']) && !empty($params['sort'])) {
  46. $sort = $params['sort'];
  47. }
  48. $order = $default_sort->default_order;
  49. if (isset($params['order']) && !empty($params['order'])) {
  50. $order = $params['order'];
  51. }
  52. if (!empty($order) && !empty($sort)) {
  53. $query->sort($sort, $order);
  54. }
  55. // Static save current search query
  56. $_query = &drupal_static('search_api_sorts_search_api_query_alter', array());
  57. $_query = $query;
  58. }

3) This function will only get applied on our desired view, if the view is different example_module_search_api_query_alter exists and the default search_api_sorts_search_api_query_alter kicks in, so your customisation breaks nothing if the view is not the one you need.

Hope this one is helpful, let me know in comments and enjoy!