search_api_autocomplete.search_api_page.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Contains code for integrating with the "Search pages" module.
  5. */
  6. /**
  7. * Implements hook_form_FORM_ID_alter().
  8. *
  9. * Adds autocompletion to the keywords field on search pages, if enabled by the
  10. * user.
  11. */
  12. function search_api_autocomplete_form_search_api_page_search_form_alter(array &$form, array &$form_state) {
  13. if (isset($form['form'])) {
  14. $form = &$form['form'];
  15. }
  16. $id = 'search_api_page_' . $form['id']['#value'];
  17. $search = search_api_autocomplete_search_load($id);
  18. if ($search && $search->enabled) {
  19. $search->alterElement($form['keys_' . $form['id']['#value']]);
  20. }
  21. }
  22. /**
  23. * Returns a list of search pages for the given index.
  24. *
  25. * @param SearchApiIndex $index
  26. * The index whose searches should be returned.
  27. *
  28. * @return array
  29. * An array of searches, keyed by their machine name. The values are arrays
  30. * with the following keys:
  31. * - name: A human-readable name for this search.
  32. * - options: (optional) An array of options to use for this search.
  33. * Type-specific options should go into the "custom" nested key in these
  34. * options.
  35. */
  36. function search_api_autocomplete_pages_searches(SearchApiIndex $index) {
  37. $ret = array();
  38. foreach (search_api_page_load_multiple(FALSE, array('index_id' => $index->machine_name)) as $page) {
  39. $id = 'search_api_page_' . $page->id;
  40. $ret[$id]['name'] = $page->name;
  41. $ret[$id]['options']['custom']['page_id'] = $page->id;
  42. }
  43. return $ret;
  44. }
  45. /**
  46. * Create the query that would be issued for the given search for the complete keys.
  47. *
  48. * @param SearchApiAutocompleteSearch $search
  49. * The search for which to create the query.
  50. * @param $complete
  51. * A string containing the complete search keys.
  52. * @param $incomplete
  53. * A string containing the incomplete last search key.
  54. *
  55. * @return SearchApiQueryInterface
  56. * The query that would normally be executed when only $complete was entered
  57. * as the search keys for the given search.
  58. */
  59. function search_api_autocomplete_pages_query(SearchApiAutocompleteSearch $search, $complete, $incomplete) {
  60. $page = search_api_page_load($search->options['custom']['page_id']);
  61. // Copied from search_api_page_search_execute().
  62. $query = search_api_query($page->index_id, array('parse mode' => $page->options['mode']))
  63. ->keys($complete);
  64. if (!empty($page->options['fields'])) {
  65. $query->fields($page->options['fields']);
  66. }
  67. return $query;
  68. }