search_api_autocomplete.search_api_views.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Contains code for integrating with the "Search views" module.
  5. */
  6. /**
  7. * Implements hook_form_FORM_ID_alter().
  8. *
  9. * Adds autocompletion to input fields for fulltext keywords on views with
  10. * exposed filters.
  11. */
  12. function search_api_autocomplete_form_views_exposed_form_alter(array &$form, array &$form_state) {
  13. $view = $form_state['view'];
  14. if (substr($view->base_table, 0, 17) != 'search_api_index_') {
  15. return;
  16. }
  17. $search_id = 'search_api_views_' . $view->name;
  18. $search = search_api_autocomplete_search_load($search_id);
  19. if (empty($search->enabled)) {
  20. return;
  21. }
  22. $index_id = substr($view->base_table, 17);
  23. $index = search_api_index_load($index_id);
  24. if (empty($index->options['fields'])) {
  25. return;
  26. }
  27. $fields = $index->getFulltextFields(TRUE);
  28. // Add the "Search: Fulltext search" filter as another text field.
  29. $fields[] = 'search_api_views_fulltext';
  30. // We need the _entity_views_field_identifier() function to translate Search
  31. // API field names into Views identifiers.
  32. module_load_include('views.inc', 'entity', 'views/entity');
  33. foreach ($fields as $search_field) {
  34. $field = _entity_views_field_identifier($search_field, array());
  35. if (!empty($view->filter[$field]->options['expose']['identifier'])) {
  36. $key = $view->filter[$field]->options['expose']['identifier'];
  37. if (isset($form[$key]) && $form[$key]['#type'] == 'textfield') {
  38. if ($field == 'search_api_views_fulltext') {
  39. $fields = $view->filter[$field]->options['fields'];
  40. }
  41. else {
  42. $fields = array($search_field);
  43. }
  44. $search->alterElement($form[$key], $fields);
  45. }
  46. }
  47. }
  48. }
  49. /**
  50. * Returns a list of search views for the given index.
  51. *
  52. * @param SearchApiIndex $index
  53. * The index whose searches should be returned.
  54. *
  55. * @return array
  56. * An array of searches, keyed by their machine name. The values are arrays
  57. * with the following keys:
  58. * - name: A human-readable name for this search.
  59. * - options: (optional) An array of options to use for this search.
  60. * Type-specific options should go into the "custom" nested key in these
  61. * options.
  62. */
  63. function search_api_autocomplete_views_searches(SearchApiIndex $index) {
  64. $ret = array();
  65. foreach (views_get_all_views() as $name => $view) {
  66. if (substr($view->base_table, 0, 17) == 'search_api_index_') {
  67. // @todo Check whether there is an exposed fulltext filter
  68. $ret['search_api_views_' . $name] = array(
  69. 'name' => $view->human_name,
  70. );
  71. }
  72. }
  73. return $ret;
  74. }
  75. /**
  76. * Create the query that would be issued for the given search for the complete keys.
  77. *
  78. * @param SearchApiAutocompleteSearch $search
  79. * The search for which to create the query.
  80. * @param $complete
  81. * A string containing the complete search keys.
  82. * @param $incomplete
  83. * A string containing the incomplete last search key.
  84. *
  85. * @return SearchApiQueryInterface
  86. * The query that would normally be executed when only $complete was entered
  87. * as the search keys for the given search.
  88. */
  89. function search_api_autocomplete_views_query(SearchApiAutocompleteSearch $search, $complete, $incomplete) {
  90. $views_id = substr($search->machine_name, 17);
  91. $view = views_get_view($views_id);
  92. // @todo Let users select display
  93. $view->set_display();
  94. // @todo Determine arguments
  95. $view->pre_execute();
  96. $view->build();
  97. $query = $view->query->getSearchApiQuery();
  98. $query->keys($complete);
  99. return $query;
  100. }