search_api_views.views.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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->getEntityType()])) {
  23. $table['table'] += array(
  24. 'entity type' => $index->getEntityType(),
  25. 'skip entity load' => TRUE,
  26. );
  27. }
  28. try {
  29. $wrapper = $index->entityWrapper(NULL, TRUE);
  30. }
  31. catch (EntityMetadataWrapperException $e) {
  32. watchdog_exception('search_api_views', $e, "%type while retrieving metadata for index %index: !message in %function (line %line of %file).", array('%index' => $index->name), WATCHDOG_WARNING);
  33. continue;
  34. }
  35. // Add field handlers and relationships provided by the Entity API.
  36. foreach ($wrapper as $key => $property) {
  37. $info = $property->info();
  38. if ($info) {
  39. entity_views_field_definition($key, $info, $table);
  40. }
  41. }
  42. // Add handlers for all indexed fields.
  43. foreach ($index->getFields() as $key => $field) {
  44. $tmp = $wrapper;
  45. $group = '';
  46. $name = '';
  47. $parts = explode(':', $key);
  48. foreach ($parts as $i => $part) {
  49. if (!isset($tmp->$part)) {
  50. continue 2;
  51. }
  52. $tmp = $tmp->$part;
  53. $info = $tmp->info();
  54. $group = ($group ? $group . ' » ' . $name : ($name ? $name : ''));
  55. $name = $info['label'];
  56. if ($i < count($parts) - 1) {
  57. // Unwrap lists.
  58. $level = search_api_list_nesting_level($info['type']);
  59. for ($j = 0; $j < $level; ++$j) {
  60. $tmp = $tmp[0];
  61. }
  62. }
  63. }
  64. $id = _entity_views_field_identifier($key, $table);
  65. if ($group) {
  66. // @todo Entity type label instead of $group?
  67. $table[$id]['group'] = $group;
  68. $name = t('@field (indexed)', array('@field' => $name));
  69. }
  70. $table[$id]['title'] = $name;
  71. $table[$id]['help'] = empty($info['description']) ? t('(No information available)') : $info['description'];
  72. $table[$id]['type'] = $field['type'];
  73. if ($id != $key) {
  74. $table[$id]['real field'] = $key;
  75. }
  76. _search_api_views_add_handlers($key, $field, $tmp, $table);
  77. }
  78. // Special handlers
  79. $table['search_api_language']['filter']['handler'] = 'SearchApiViewsHandlerFilterLanguage';
  80. $table['search_api_id']['title'] = t('Entity ID');
  81. $table['search_api_id']['help'] = t("The entity's ID.");
  82. $table['search_api_id']['sort']['handler'] = 'SearchApiViewsHandlerSort';
  83. $table['search_api_relevance']['group'] = t('Search');
  84. $table['search_api_relevance']['title'] = t('Relevance');
  85. $table['search_api_relevance']['help'] = t('The relevance of this search result with respect to the query.');
  86. $table['search_api_relevance']['field']['type'] = 'decimal';
  87. $table['search_api_relevance']['field']['handler'] = 'entity_views_handler_field_numeric';
  88. $table['search_api_relevance']['field']['click sortable'] = TRUE;
  89. $table['search_api_relevance']['sort']['handler'] = 'SearchApiViewsHandlerSort';
  90. $table['search_api_excerpt']['group'] = t('Search');
  91. $table['search_api_excerpt']['title'] = t('Excerpt');
  92. $table['search_api_excerpt']['help'] = t('The search result excerpted to show found search terms.');
  93. $table['search_api_excerpt']['field']['type'] = 'text';
  94. $table['search_api_excerpt']['field']['handler'] = 'entity_views_handler_field_text';
  95. $table['search_api_views_fulltext']['group'] = t('Search');
  96. $table['search_api_views_fulltext']['title'] = t('Fulltext search');
  97. $table['search_api_views_fulltext']['help'] = t('Search several or all fulltext fields at once.');
  98. $table['search_api_views_fulltext']['filter']['handler'] = 'SearchApiViewsHandlerFilterFulltext';
  99. $table['search_api_views_fulltext']['argument']['handler'] = 'SearchApiViewsHandlerArgumentFulltext';
  100. $table['search_api_views_more_like_this']['group'] = t('Search');
  101. $table['search_api_views_more_like_this']['title'] = t('More like this');
  102. $table['search_api_views_more_like_this']['help'] = t('Find similar content.');
  103. $table['search_api_views_more_like_this']['argument']['handler'] = 'SearchApiViewsHandlerArgumentMoreLikeThis';
  104. // If there are taxonomy term references indexed in the index, include the
  105. // "Indexed taxonomy term fields" contextual filter. We also save for all
  106. // fields whether they contain only terms of a certain vocabulary, keying
  107. // that information by vocabulary for later ease of use.
  108. $vocabulary_fields = array();
  109. foreach ($index->getFields() as $key => $field) {
  110. if (isset($field['entity_type']) && $field['entity_type'] === 'taxonomy_term') {
  111. $field_id = ($pos = strrpos($key, ':')) ? substr($key, $pos + 1) : $key;
  112. $field_info = field_info_field($field_id);
  113. if (isset($field_info['settings']['allowed_values'][0]['vocabulary'])) {
  114. $vocabulary_fields[$field_info['settings']['allowed_values'][0]['vocabulary']][] = $key;
  115. }
  116. else {
  117. $vocabulary_fields[''][] = $key;
  118. }
  119. }
  120. }
  121. if ($vocabulary_fields) {
  122. $table['search_api_views_taxonomy_term']['group'] = t('Search');
  123. $table['search_api_views_taxonomy_term']['title'] = t('Indexed taxonomy term fields');
  124. $table['search_api_views_taxonomy_term']['help'] = t('Search in all indexed taxonomy term fields.');
  125. $table['search_api_views_taxonomy_term']['argument']['handler'] = 'SearchApiViewsHandlerArgumentTaxonomyTerm';
  126. $table['search_api_views_taxonomy_term']['argument']['vocabulary_fields'] = $vocabulary_fields;
  127. }
  128. }
  129. return $data;
  130. }
  131. catch (Exception $e) {
  132. watchdog_exception('search_api_views', $e);
  133. }
  134. }
  135. /**
  136. * Helper function that returns an array of handler definitions to add to a
  137. * views field definition.
  138. */
  139. function _search_api_views_add_handlers($id, array $field, EntityMetadataWrapper $wrapper, array &$table) {
  140. $type = $field['type'];
  141. $inner_type = search_api_extract_inner_type($type);
  142. if (strpos($id, ':')) {
  143. entity_views_field_definition($id, $wrapper->info(), $table);
  144. }
  145. $id = _entity_views_field_identifier($id, $table);
  146. $table += array($id => array());
  147. if ($inner_type == 'text') {
  148. $table[$id] += array(
  149. 'argument' => array(
  150. 'handler' => 'SearchApiViewsHandlerArgument',
  151. ),
  152. 'filter' => array(
  153. 'handler' => 'SearchApiViewsHandlerFilterText',
  154. ),
  155. );
  156. return;
  157. }
  158. if ($options = $wrapper->optionsList('view')) {
  159. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterOptions';
  160. $table[$id]['filter']['options'] = $options;
  161. $table[$id]['filter']['multi-valued'] = search_api_is_list_type($type);
  162. }
  163. elseif ($inner_type == 'boolean') {
  164. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterBoolean';
  165. }
  166. elseif ($inner_type == 'date') {
  167. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterDate';
  168. }
  169. else {
  170. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilter';
  171. }
  172. if ($inner_type == 'string' || $inner_type == 'uri') {
  173. $table[$id]['argument']['handler'] = 'SearchApiViewsHandlerArgumentString';
  174. }
  175. else {
  176. $table[$id]['argument']['handler'] = 'SearchApiViewsHandlerArgument';
  177. }
  178. // We can only sort according to single-valued fields.
  179. if ($type == $inner_type) {
  180. $table[$id]['sort']['handler'] = 'SearchApiViewsHandlerSort';
  181. if (isset($table[$id]['field'])) {
  182. $table[$id]['field']['click sortable'] = TRUE;
  183. }
  184. }
  185. }
  186. /**
  187. * Implements hook_views_plugins().
  188. */
  189. function search_api_views_views_plugins() {
  190. // Collect all base tables provided by this module.
  191. $bases = array();
  192. foreach (search_api_index_load_multiple(FALSE) as $index) {
  193. $bases[] = 'search_api_index_' . $index->machine_name;
  194. }
  195. $ret = array(
  196. 'query' => array(
  197. 'search_api_views_query' => array(
  198. 'title' => t('Search API Query'),
  199. 'help' => t('Query will be generated and run using the Search API.'),
  200. 'handler' => 'SearchApiViewsQuery',
  201. ),
  202. ),
  203. 'cache' => array(
  204. 'search_api_views_cache' => array(
  205. 'title' => t('Search-specific'),
  206. 'help' => t("Cache Search API views. (Other methods probably won't work with search views.)"),
  207. 'base' => $bases,
  208. 'handler' => 'SearchApiViewsCache',
  209. 'uses options' => TRUE,
  210. ),
  211. ),
  212. );
  213. if (module_exists('search_api_facetapi')) {
  214. $ret['display']['search_api_views_facets_block'] = array(
  215. 'title' => t('Facets block'),
  216. 'help' => t('Display facets for this search as a block anywhere on the site.'),
  217. 'handler' => 'SearchApiViewsFacetsBlockDisplay',
  218. 'uses hook block' => TRUE,
  219. 'use ajax' => FALSE,
  220. 'use pager' => FALSE,
  221. 'use more' => TRUE,
  222. 'accept attachments' => TRUE,
  223. 'admin' => t('Facets block'),
  224. );
  225. }
  226. return $ret;
  227. }