-
Notifications
You must be signed in to change notification settings - Fork 14
/
search_api_solr.views.inc
74 lines (66 loc) · 2.22 KB
/
search_api_solr.views.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* @file
* Views integration code for the Search API Solr module.
*/
use Drupal\search_api\Exception\SearchApiException;
/**
* Implements hook_views_data_alter().
*
* Adds field handlers for "virtual" fields, if the index's Solr server has
* "Retrieve results data from Solr" enabled.
*
* @todo Update!
*/
function _search_api_solr_views_data_alter(array &$data) {
try {
foreach (entity_load_multiple('search_api_index') as $index) {
/** @var \Drupal\search_api\IndexInterface $index */
$server = NULL;
try {
$server = $index->getServer();
}
catch (SearchApiException $e) {
// Just ignore invalid servers and skip the index.
}
if (!$server || empty($server->options['retrieve_data'])) {
return;
}
// Fill in base data.
$key = 'search_api_index_' . $index->machine_name;
$table = & $data[$key];
try {
$wrapper = $index->entityWrapper(NULL, FALSE);
}
catch (EntityMetadataWrapperException $e) {
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);
continue;
}
// Remember fields that aren't added by data alterations, etc. (since
// there isn't any other way to tell them apart).
$normal_fields = array();
foreach ($wrapper as $key => $property) {
$normal_fields[$key] = TRUE;
}
try {
$wrapper = $index->entityWrapper(NULL);
}
catch (EntityMetadataWrapperException $e) {
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);
continue;
}
// Add field handlers for items added by data alterations, etc.
foreach ($wrapper as $key => $property) {
if (empty($normal_fields[$key])) {
$info = $property->info();
if ($info) {
entity_views_field_definition($key, $info, $table);
}
}
}
}
}
catch (Exception $e) {
watchdog_exception('search_api_views', $e);
}
}