search_api_views.views.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Implements hook_views_data().
  4. */
  5. function search_api_views_views_data() {
  6. try {
  7. $data = array();
  8. $entity_types = entity_get_info();
  9. foreach (search_api_index_load_multiple(FALSE) as $index) {
  10. // Fill in base data.
  11. $key = 'search_api_index_' . $index->machine_name;
  12. $table = &$data[$key];
  13. $type_info = search_api_get_item_type_info($index->item_type);
  14. $table['table']['group'] = t('Indexed @entity_type', array('@entity_type' => $type_info['name']));
  15. $table['table']['base'] = array(
  16. 'field' => 'search_api_id',
  17. 'index' => $index->machine_name,
  18. 'title' => $index->name,
  19. 'help' => t('Use the %name search index for filtering and retrieving data.', array('%name' => $index->name)),
  20. 'query class' => 'search_api_views_query',
  21. );
  22. if (isset($entity_types[$index->item_type])) {
  23. $table['table'] += array(
  24. 'entity type' => $index->item_type,
  25. 'skip entity load' => TRUE,
  26. );
  27. }
  28. $wrapper = $index->entityWrapper(NULL, TRUE);
  29. // Add field handlers and relationships provided by the Entity API.
  30. foreach ($wrapper as $key => $property) {
  31. $info = $property->info();
  32. if ($info) {
  33. entity_views_field_definition($key, $info, $table);
  34. }
  35. }
  36. // Add handlers for all indexed fields.
  37. foreach ($index->getFields() as $key => $field) {
  38. $tmp = $wrapper;
  39. $group = '';
  40. $name = '';
  41. $parts = explode(':', $key);
  42. foreach ($parts as $i => $part) {
  43. if (!isset($tmp->$part)) {
  44. continue 2;
  45. }
  46. $tmp = $tmp->$part;
  47. $info = $tmp->info();
  48. $group = ($group ? $group . ' » ' . $name : ($name ? $name : ''));
  49. $name = $info['label'];
  50. if ($i < count($parts) - 1) {
  51. // Unwrap lists.
  52. $level = search_api_list_nesting_level($info['type']);
  53. for ($j = 0; $j < $level; ++$j) {
  54. $tmp = $tmp[0];
  55. }
  56. }
  57. }
  58. $id = _entity_views_field_identifier($key, $table);
  59. if ($group) {
  60. // @todo Entity type label instead of $group?
  61. $table[$id]['group'] = $group;
  62. $name = t('@field (indexed)', array('@field' => $name));
  63. }
  64. $table[$id]['title'] = $name;
  65. $table[$id]['help'] = empty($info['description']) ? t('(No information available)') : $info['description'];
  66. $table[$id]['type'] = $field['type'];
  67. if ($id != $key) {
  68. $table[$id]['real field'] = $key;
  69. }
  70. _search_api_views_add_handlers($key, $field, $tmp, $table);
  71. }
  72. // Special handlers
  73. $table['search_api_language']['filter']['handler'] = 'SearchApiViewsHandlerFilterLanguage';
  74. $table['search_api_id']['title'] = t('Entity ID');
  75. $table['search_api_id']['help'] = t("The entity's ID.");
  76. $table['search_api_id']['sort']['handler'] = 'SearchApiViewsHandlerSort';
  77. $table['search_api_relevance']['group'] = t('Search');
  78. $table['search_api_relevance']['title'] = t('Relevance');
  79. $table['search_api_relevance']['help'] = t('The relevance of this search result with respect to the query.');
  80. $table['search_api_relevance']['field']['type'] = 'decimal';
  81. $table['search_api_relevance']['field']['handler'] = 'entity_views_handler_field_numeric';
  82. $table['search_api_relevance']['field']['click sortable'] = TRUE;
  83. $table['search_api_relevance']['sort']['handler'] = 'SearchApiViewsHandlerSort';
  84. $table['search_api_excerpt']['group'] = t('Search');
  85. $table['search_api_excerpt']['title'] = t('Excerpt');
  86. $table['search_api_excerpt']['help'] = t('The search result excerpted to show found search terms.');
  87. $table['search_api_excerpt']['field']['type'] = 'text';
  88. $table['search_api_excerpt']['field']['handler'] = 'entity_views_handler_field_text';
  89. $table['search_api_views_fulltext']['group'] = t('Search');
  90. $table['search_api_views_fulltext']['title'] = t('Fulltext search');
  91. $table['search_api_views_fulltext']['help'] = t('Search several or all fulltext fields at once.');
  92. $table['search_api_views_fulltext']['filter']['handler'] = 'SearchApiViewsHandlerFilterFulltext';
  93. $table['search_api_views_fulltext']['argument']['handler'] = 'SearchApiViewsHandlerArgumentFulltext';
  94. $table['search_api_views_more_like_this']['group'] = t('Search');
  95. $table['search_api_views_more_like_this']['title'] = t('More like this');
  96. $table['search_api_views_more_like_this']['help'] = t('Find similar content.');
  97. $table['search_api_views_more_like_this']['argument']['handler'] = 'SearchApiViewsHandlerArgumentMoreLikeThis';
  98. }
  99. return $data;
  100. }
  101. catch (Exception $e) {
  102. watchdog_exception('search_api_views', $e);
  103. }
  104. }
  105. /**
  106. * Helper function that returns an array of handler definitions to add to a
  107. * views field definition.
  108. */
  109. function _search_api_views_add_handlers($id, array $field, EntityMetadataWrapper $wrapper, array &$table) {
  110. $type = $field['type'];
  111. $inner_type = search_api_extract_inner_type($type);
  112. if (strpos($id, ':')) {
  113. entity_views_field_definition($id, $wrapper->info(), $table);
  114. }
  115. $id = _entity_views_field_identifier($id, $table);
  116. $table += array($id => array());
  117. if ($inner_type == 'text') {
  118. $table[$id] += array(
  119. 'argument' => array(
  120. 'handler' => 'SearchApiViewsHandlerArgumentText',
  121. ),
  122. 'filter' => array(
  123. 'handler' => 'SearchApiViewsHandlerFilterText',
  124. ),
  125. );
  126. return;
  127. }
  128. if ($options = $wrapper->optionsList('view')) {
  129. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterOptions';
  130. $table[$id]['filter']['options'] = $options;
  131. }
  132. elseif ($inner_type == 'boolean') {
  133. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterBoolean';
  134. }
  135. elseif ($inner_type == 'date') {
  136. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterDate';
  137. }
  138. else {
  139. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilter';
  140. }
  141. $table[$id]['argument']['handler'] = 'SearchApiViewsHandlerArgument';
  142. // We can only sort according to single-valued fields.
  143. if ($type == $inner_type) {
  144. $table[$id]['sort']['handler'] = 'SearchApiViewsHandlerSort';
  145. if (isset($table[$id]['field'])) {
  146. $table[$id]['field']['click sortable'] = TRUE;
  147. }
  148. }
  149. }
  150. /**
  151. * Implements hook_views_plugins().
  152. */
  153. function search_api_views_views_plugins() {
  154. $ret = array(
  155. 'query' => array(
  156. 'search_api_views_query' => array(
  157. 'title' => t('Search API Query'),
  158. 'help' => t('Query will be generated and run using the Search API.'),
  159. 'handler' => 'SearchApiViewsQuery'
  160. ),
  161. ),
  162. );
  163. if (module_exists('search_api_facetapi')) {
  164. $ret['display']['search_api_views_facets_block'] = array(
  165. 'title' => t('Facets block'),
  166. 'help' => t('Display facets for this search as a block anywhere on the site.'),
  167. 'handler' => 'SearchApiViewsFacetsBlockDisplay',
  168. 'uses hook block' => TRUE,
  169. 'use ajax' => FALSE,
  170. 'use pager' => FALSE,
  171. 'use more' => TRUE,
  172. 'accept attachments' => TRUE,
  173. 'admin' => t('Facets block'),
  174. );
  175. }
  176. return $ret;
  177. }