function apachesolr_reference_autocomplete_callback_get_matches in Apachesolr Reference 7
Return JSON based on given field, instance and string.
Parameters
array $field: The field array defintion.
array $instance: The instance array defintion.
string $entity_type: The entity type.
int|string $entity_id: Optional; The entity ID the entity-reference field is attached to.
string $langcode: Optional: The language to use.
string $string: The label of the entity to query by.
Return value
json A json array of results.
1 call to apachesolr_reference_autocomplete_callback_get_matches()
- apachesolr_reference_autocomplete_callback in ./
apachesolr_reference.module - Menu callback: autocomplete the label of a SOLR object.
File
- ./
apachesolr_reference.module, line 97 - functionality for creating reference fields to apache solr objects.
Code
function apachesolr_reference_autocomplete_callback_get_matches($field, $instance, $entity_type, $entity_id = '', $langcode = LANGUAGE_NONE, $string = '') {
$matches = array();
$entity = NULL;
if ($entity_id !== 'NULL') {
$entity = entity_load_single($entity_type, $entity_id);
if (!$entity || !entity_access('view', $entity_type, $entity)) {
return MENU_ACCESS_DENIED;
}
}
// Was a value passed to lookup?
if (isset($string)) {
$string = trim($string);
$string = str_replace('"', '', $string);
// Retrieve the values we need to know which solr instance to use.
$solr_env = _apachesolr_reference_default_solr_environment($field['settings']);
$search_fields = $field['settings']['search_fields'];
// Build the SOLR query.
$field_query = array(
$field['settings']['field_query'],
);
$field_query[] = $search_fields['label'] . ':("' . $string . '" OR ' . $string . '*)';
// If Entity and language is not und, add language filter.
if ($langcode && $langcode != LANGUAGE_NONE) {
$field_query[] = 'ss_language: ' . $langcode;
}
// List of SOLR fields to return.
$field_list = $search_fields;
// Sort SOLR results by the label value.
$sort = array(
$search_fields['label'],
'asc',
);
// Query solr to retrieve data for objects.
if ($solr_objects = _apachesolr_reference_solr_query($solr_env, $field_query, $field_list, $sort)) {
// Foreach returned SOLR object add to matches array.
foreach ($solr_objects as $solr_object) {
$id = $solr_object->{$search_fields}['id'];
$label = $solr_object->{$search_fields}['label'];
$key = $label . '(' . $id . ')';
$matches[$key] = '<div class="reference-autocomplete">' . $label . '</div>';
}
}
}
drupal_json_output($matches);
}