-
Notifications
You must be signed in to change notification settings - Fork 0
/
apachesolr_asset.module
105 lines (94 loc) · 3.17 KB
/
apachesolr_asset.module
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* @file
* Indexer for the userhook_apachesolr_entity_info_alter entities.
*/
/**
* Implements hook_entity_info_alter().
*/
function apachesolr_asset_apachesolr_entity_info_alter(&$entity_info) {
$entity_info['asset']['indexable'] = TRUE;
$entity_info['asset']['status callback'] = 'apachesolr_asset_status_callback';
$entity_info['asset']['document callback'][] = 'apachesolr_asset_solr_document';
$entity_info['asset']['reindex callback'] = 'apachesolr_asset_solr_reindex';
$entity_info['asset']['index_table'] = 'apachesolr_index_entities_asset';
}
/**
* Builds the user-specific information for a Solr document.
*
* @param ApacheSolrDocument $document
* The Solr document we are building up.
* @param stdClass $asset
* The entity we are indexing.
* @param string $entity_type
* The type of entity we're dealing with.
*/
function apachesolr_asset_solr_document(ApacheSolrDocument $document, $asset, $entity_type) {
$document->label = apachesolr_clean_text($asset->title);
$document->entity_type = $entity_type;
$document->entity_id = $asset->aid;
$build = $asset->view('search_index');
$text = drupal_render($build);
$document->content = apachesolr_clean_text($text);
$documents = array();
$documents[] = $document;
return $documents;
}
/**
* Reindexing callback for ApacheSolr, for assets.
*/
function apachesolr_asset_solr_reindex($env_id, $bundle = NULL) {
$indexer_table = apachesolr_get_indexer_table('asset');
$transaction = db_transaction();
try {
$delete = db_delete($indexer_table);
$delete->condition('entity_type', 'asset');
if (!empty($bundle)) {
$delete->condition('bundle', $bundle);
}
$delete->execute();
$indexable_bundles = apachesolr_get_index_bundles($env_id, 'asset');
if ($bundle && !empty($indexable_bundles) && !in_array($bundle, $indexable_bundles)) {
// The bundle specified is not in the indexable bundles list.
return;
}
// Select the assets to be indexed.
$select = db_select('asset', 'a');
$select->addExpression("'asset'", 'entity_type');
$select->addField('a', 'aid', 'entity_id');
$select->addField('a', 'type', 'bundle');
$select->addExpression(REQUEST_TIME, 'changed');
if ($bundle) {
// Mark all assets of the specified type for reindexing.
$select->condition('a.type', $bundle);
}
elseif (!empty($indexable_bundles)) {
// Restrict reindex to types in the indexable bundles list.
$select->condition('a.type', $indexable_bundles, 'IN');
}
// Insert the selected asset data into the index table.
$insert = db_insert($indexer_table)
->fields(array('entity_id', 'bundle', 'entity_type', 'changed'))
->from($select)
->execute();
}
catch (Exception $e) {
$transaction->rollback();
throw $e;
}
}
/**
* Status callback for ApacheSolr, for assets.
*
* @param int $entity_id
* ID of the asset to check.
* @param string $entity_type
* The string "asset".
*
* @return boolean
* Truthy when the entity should be public in search results.
*/
function apachesolr_asset_status_callback($entity_id, $entity_type) {
// All assets should be visible to all users.
return TRUE;
}