search_api_views.views.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * @file
  4. * Views hook implementations for the Search API Views module.
  5. */
  6. /**
  7. * Implements hook_views_data().
  8. */
  9. function search_api_views_views_data() {
  10. try {
  11. $data = array();
  12. $entity_types = entity_get_info();
  13. foreach (search_api_index_load_multiple(FALSE) as $index) {
  14. // Fill in base data.
  15. $key = 'search_api_index_' . $index->machine_name;
  16. $table = &$data[$key];
  17. $type_info = search_api_get_item_type_info($index->item_type);
  18. $table['table']['group'] = t('Indexed @entity_type', array('@entity_type' => $type_info['name']));
  19. $table['table']['base'] = array(
  20. 'field' => 'search_api_id',
  21. 'index' => $index->machine_name,
  22. 'title' => $index->name,
  23. 'help' => t('Use the %name search index for filtering and retrieving data.', array('%name' => $index->name)),
  24. 'query class' => 'search_api_views_query',
  25. );
  26. if (isset($entity_types[$index->getEntityType()])) {
  27. $table['table'] += array(
  28. 'entity type' => $index->getEntityType(),
  29. 'skip entity load' => TRUE,
  30. );
  31. }
  32. try {
  33. $wrapper = $index->entityWrapper(NULL, FALSE);
  34. }
  35. catch (EntityMetadataWrapperException $e) {
  36. 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);
  37. continue;
  38. }
  39. // Add field handlers and relationships provided by the Entity API.
  40. foreach ($wrapper as $key => $property) {
  41. $info = $property->info();
  42. if ($info) {
  43. entity_views_field_definition($key, $info, $table);
  44. }
  45. }
  46. try {
  47. $wrapper = $index->entityWrapper(NULL);
  48. }
  49. catch (EntityMetadataWrapperException $e) {
  50. 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);
  51. continue;
  52. }
  53. // Add handlers for all indexed fields.
  54. foreach ($index->getFields() as $key => $field) {
  55. $tmp = $wrapper;
  56. $group = '';
  57. $name = '';
  58. $parts = explode(':', $key);
  59. foreach ($parts as $i => $part) {
  60. if (!isset($tmp->$part)) {
  61. continue 2;
  62. }
  63. $tmp = $tmp->$part;
  64. $info = $tmp->info();
  65. $group = ($group ? $group . ' » ' . $name : ($name ? $name : ''));
  66. $name = $info['label'];
  67. if ($i < count($parts) - 1) {
  68. // Unwrap lists.
  69. $level = search_api_list_nesting_level($info['type']);
  70. for ($j = 0; $j < $level; ++$j) {
  71. $tmp = $tmp[0];
  72. }
  73. }
  74. }
  75. $id = _entity_views_field_identifier($key, $table);
  76. if ($group) {
  77. // @todo Entity type label instead of $group?
  78. $table[$id]['group'] = $group;
  79. $name = t('!field (indexed)', array('!field' => $name));
  80. }
  81. $table[$id]['title'] = $name;
  82. $table[$id]['help'] = empty($info['description']) ? t('(No information available)') : $info['description'];
  83. $table[$id]['type'] = $field['type'];
  84. if ($id != $key) {
  85. $table[$id]['real field'] = $key;
  86. }
  87. _search_api_views_add_handlers($key, $field, $tmp, $table);
  88. }
  89. // Special handlers
  90. $table['search_api_language']['filter']['handler'] = 'SearchApiViewsHandlerFilterLanguage';
  91. $table['search_api_id']['title'] = t('Entity ID');
  92. $table['search_api_id']['help'] = t("The entity's ID.");
  93. $table['search_api_id']['sort']['handler'] = 'SearchApiViewsHandlerSort';
  94. $table['search_api_relevance']['group'] = t('Search');
  95. $table['search_api_relevance']['title'] = t('Relevance');
  96. $table['search_api_relevance']['help'] = t('The relevance of this search result with respect to the query.');
  97. $table['search_api_relevance']['field']['type'] = 'decimal';
  98. $table['search_api_relevance']['field']['handler'] = 'entity_views_handler_field_numeric';
  99. $table['search_api_relevance']['field']['click sortable'] = TRUE;
  100. $table['search_api_relevance']['sort']['handler'] = 'SearchApiViewsHandlerSort';
  101. $table['search_api_excerpt']['group'] = t('Search');
  102. $table['search_api_excerpt']['title'] = t('Excerpt');
  103. $table['search_api_excerpt']['help'] = t('The search result excerpted to show found search terms.');
  104. $table['search_api_excerpt']['field']['type'] = 'text';
  105. $table['search_api_excerpt']['field']['handler'] = 'entity_views_handler_field_text';
  106. $table['search_api_views_fulltext']['group'] = t('Search');
  107. $table['search_api_views_fulltext']['title'] = t('Fulltext search');
  108. $table['search_api_views_fulltext']['help'] = t('Search several or all fulltext fields at once.');
  109. $table['search_api_views_fulltext']['filter']['handler'] = 'SearchApiViewsHandlerFilterFulltext';
  110. $table['search_api_views_fulltext']['argument']['handler'] = 'SearchApiViewsHandlerArgumentFulltext';
  111. $table['search_api_views_more_like_this']['group'] = t('Search');
  112. $table['search_api_views_more_like_this']['title'] = t('More like this');
  113. $table['search_api_views_more_like_this']['help'] = t('Find similar content.');
  114. $table['search_api_views_more_like_this']['argument']['handler'] = 'SearchApiViewsHandlerArgumentMoreLikeThis';
  115. // If there are taxonomy term references indexed in the index, include the
  116. // "Indexed taxonomy term fields" contextual filter. We also save for all
  117. // fields whether they contain only terms of a certain vocabulary, keying
  118. // that information by vocabulary for later ease of use.
  119. $vocabulary_fields = array();
  120. foreach ($index->getFields() as $key => $field) {
  121. if (isset($field['entity_type']) && $field['entity_type'] === 'taxonomy_term') {
  122. $field_id = ($pos = strrpos($key, ':')) ? substr($key, $pos + 1) : $key;
  123. $field_info = field_info_field($field_id);
  124. if (isset($field_info['settings']['allowed_values'][0]['vocabulary'])) {
  125. $vocabulary_fields[$field_info['settings']['allowed_values'][0]['vocabulary']][] = $key;
  126. }
  127. else {
  128. $vocabulary_fields[''][] = $key;
  129. }
  130. }
  131. }
  132. if ($vocabulary_fields) {
  133. $table['search_api_views_taxonomy_term']['group'] = t('Search');
  134. $table['search_api_views_taxonomy_term']['title'] = t('Indexed taxonomy term fields');
  135. $table['search_api_views_taxonomy_term']['help'] = t('Search in all indexed taxonomy term fields.');
  136. $table['search_api_views_taxonomy_term']['argument']['handler'] = 'SearchApiViewsHandlerArgumentTaxonomyTerm';
  137. $table['search_api_views_taxonomy_term']['argument']['vocabulary_fields'] = $vocabulary_fields;
  138. }
  139. }
  140. return $data;
  141. }
  142. catch (Exception $e) {
  143. watchdog_exception('search_api_views', $e);
  144. }
  145. }
  146. /**
  147. * Adds handler definitions for a field to a Views data table definition.
  148. *
  149. * Helper method for search_api_views_views_data().
  150. *
  151. * @param $id
  152. * The internal identifier of the field.
  153. * @param array $field
  154. * Information about the field.
  155. * @param EntityMetadataWrapper $wrapper
  156. * A wrapper providing further metadata about the field.
  157. * @param array $table
  158. * The existing Views data table definition, as a reference.
  159. */
  160. function _search_api_views_add_handlers($id, array $field, EntityMetadataWrapper $wrapper, array &$table) {
  161. $type = $field['type'];
  162. $inner_type = search_api_extract_inner_type($type);
  163. if (strpos($id, ':')) {
  164. entity_views_field_definition($id, $wrapper->info(), $table);
  165. }
  166. $id = _entity_views_field_identifier($id, $table);
  167. $table += array($id => array());
  168. if ($inner_type == 'text') {
  169. $table[$id] += array(
  170. 'argument' => array(
  171. 'handler' => 'SearchApiViewsHandlerArgument',
  172. ),
  173. 'filter' => array(
  174. 'handler' => 'SearchApiViewsHandlerFilterText',
  175. ),
  176. );
  177. return;
  178. }
  179. $info = $wrapper->info();
  180. if (isset($info['options list']) && is_callable($info['options list'])) {
  181. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterOptions';
  182. $table[$id]['filter']['multi-valued'] = search_api_is_list_type($type);
  183. }
  184. elseif ($inner_type == 'boolean') {
  185. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterBoolean';
  186. }
  187. elseif ($inner_type == 'date') {
  188. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterDate';
  189. }
  190. elseif (isset($field['entity_type']) && $field['entity_type'] === 'user') {
  191. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterUser';
  192. }
  193. elseif (isset($field['entity_type']) && $field['entity_type'] === 'taxonomy_term') {
  194. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilterTaxonomyTerm';
  195. $info = $wrapper->info();
  196. $field_info = field_info_field($info['name']);
  197. // For the "Parent terms" and "All parent terms" properties, we can
  198. // extrapolate the vocabulary from the parent in the selector. (E.g.,
  199. // for "field_tags:parent" we can use the information of "field_tags".)
  200. // Otherwise, we can't include any vocabulary information.
  201. if (!$field_info && ($info['name'] == 'parent' || $info['name'] == 'parents_all')) {
  202. if (!empty($table[$id]['real field'])) {
  203. $parts = explode(':', $table[$id]['real field']);
  204. $field_info = field_info_field($parts[count($parts) - 2]);
  205. }
  206. }
  207. if (isset($field_info['settings']['allowed_values'][0]['vocabulary'])) {
  208. $table[$id]['filter']['vocabulary'] = $field_info['settings']['allowed_values'][0]['vocabulary'];
  209. }
  210. }
  211. else {
  212. $table[$id]['filter']['handler'] = 'SearchApiViewsHandlerFilter';
  213. }
  214. if ($inner_type == 'string' || $inner_type == 'uri') {
  215. $table[$id]['argument']['handler'] = 'SearchApiViewsHandlerArgumentString';
  216. }
  217. elseif ($inner_type == 'date') {
  218. $table[$id]['argument']['handler'] = 'SearchApiViewsHandlerArgumentDate';
  219. }
  220. else {
  221. $table[$id]['argument']['handler'] = 'SearchApiViewsHandlerArgument';
  222. }
  223. // We can only sort according to single-valued fields.
  224. if ($type == $inner_type) {
  225. $table[$id]['sort']['handler'] = 'SearchApiViewsHandlerSort';
  226. if (isset($table[$id]['field'])) {
  227. $table[$id]['field']['click sortable'] = TRUE;
  228. }
  229. }
  230. }
  231. /**
  232. * Implements hook_views_plugins().
  233. */
  234. function search_api_views_views_plugins() {
  235. // Collect all base tables provided by this module.
  236. $bases = array();
  237. foreach (search_api_index_load_multiple(FALSE) as $index) {
  238. $bases[] = 'search_api_index_' . $index->machine_name;
  239. }
  240. $ret = array(
  241. 'query' => array(
  242. 'search_api_views_query' => array(
  243. 'title' => t('Search API Query'),
  244. 'help' => t('Query will be generated and run using the Search API.'),
  245. 'handler' => 'SearchApiViewsQuery',
  246. ),
  247. ),
  248. 'cache' => array(
  249. 'search_api_views_cache' => array(
  250. 'title' => t('Search-specific'),
  251. 'help' => t("Cache Search API views. (Other methods probably won't work with search views.)"),
  252. 'base' => $bases,
  253. 'handler' => 'SearchApiViewsCache',
  254. 'uses options' => TRUE,
  255. ),
  256. ),
  257. );
  258. if (module_exists('search_api_facetapi')) {
  259. $ret['display']['search_api_views_facets_block'] = array(
  260. 'title' => t('Facets block'),
  261. 'help' => t('Display facets for this search as a block anywhere on the site.'),
  262. 'handler' => 'SearchApiViewsFacetsBlockDisplay',
  263. 'uses hook block' => TRUE,
  264. 'use ajax' => FALSE,
  265. 'use pager' => FALSE,
  266. 'use more' => TRUE,
  267. 'accept attachments' => TRUE,
  268. 'admin' => t('Facets block'),
  269. );
  270. }
  271. return $ret;
  272. }