search_api_solr.views.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @file
  4. * Views integration code for the Search API Solr module.
  5. */
  6. /**
  7. * Implements hook_views_data_alter().
  8. *
  9. * Adds field handlers for "virtual" fields, if the index's Solr server has
  10. * "Retrieve results data from Solr" enabled.
  11. */
  12. function search_api_solr_views_data_alter(array &$data) {
  13. try {
  14. foreach (search_api_index_load_multiple(FALSE) as $index) {
  15. $server = NULL;
  16. try {
  17. $server = $index->server();
  18. }
  19. catch (SearchApiException $e) {
  20. // Just ignore invalid servers and skip the index.
  21. }
  22. if (!$server || empty($server->options['retrieve_data'])) {
  23. continue;
  24. }
  25. // Fill in base data.
  26. $key = 'search_api_index_' . $index->machine_name;
  27. $table = & $data[$key];
  28. try {
  29. $wrapper = $index->entityWrapper(NULL, FALSE);
  30. }
  31. catch (EntityMetadataWrapperException $e) {
  32. watchdog_exception('search_api_solr', $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. // Remember fields that aren't added by data alterations, etc. (since
  36. // there isn't any other way to tell them apart).
  37. $normal_fields = array();
  38. foreach ($wrapper as $key => $property) {
  39. $normal_fields[$key] = TRUE;
  40. }
  41. try {
  42. $wrapper = $index->entityWrapper(NULL);
  43. }
  44. catch (EntityMetadataWrapperException $e) {
  45. watchdog_exception('search_api_solr', $e, "%type while retrieving metadata for index %index: !message in %function (line %line of %file).", array('%index' => $index->name), WATCHDOG_WARNING);
  46. continue;
  47. }
  48. // Add field handlers for items added by data alterations, etc.
  49. foreach ($wrapper as $key => $property) {
  50. if (empty($normal_fields[$key])) {
  51. $info = $property->info();
  52. if ($info) {
  53. entity_views_field_definition($key, $info, $table);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. catch (Exception $e) {
  60. watchdog_exception('search_api_views', $e);
  61. }
  62. }