search_api_solr.views.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = $index->server();
  16. if (!$server || empty($server->options['retrieve_data'])) {
  17. return;
  18. }
  19. // Fill in base data.
  20. $key = 'search_api_index_' . $index->machine_name;
  21. $table = & $data[$key];
  22. try {
  23. $wrapper = $index->entityWrapper(NULL, FALSE);
  24. }
  25. catch (EntityMetadataWrapperException $e) {
  26. 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);
  27. continue;
  28. }
  29. // Remember fields that aren't added by data alterations, etc. (since
  30. // there isn't any other way to tell them apart).
  31. $normal_fields = array();
  32. foreach ($wrapper as $key => $property) {
  33. $normal_fields[$key] = TRUE;
  34. }
  35. try {
  36. $wrapper = $index->entityWrapper(NULL);
  37. }
  38. catch (EntityMetadataWrapperException $e) {
  39. 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);
  40. continue;
  41. }
  42. // Add field handlers for items added by data alterations, etc.
  43. foreach ($wrapper as $key => $property) {
  44. if (empty($normal_fields[$key])) {
  45. $info = $property->info();
  46. if ($info) {
  47. entity_views_field_definition($key, $info, $table);
  48. }
  49. }
  50. }
  51. }
  52. }
  53. catch (Exception $e) {
  54. watchdog_exception('search_api_views', $e);
  55. }
  56. }