protected function Highlight::highlightFields in Search API 8
Retrieves highlighted field values for the given result items.
Parameters
\Drupal\search_api\Item\ItemInterface[] $results: The result items whose fields should be highlighted.
array $keys: The search keys to use for highlighting.
Return value
string[][][] An array keyed by item IDs, containing arrays that map field IDs to the highlighted versions of the values for that field.
1 call to Highlight::highlightFields()
- Highlight::postprocessSearchResults in src/
Plugin/ search_api/ processor/ Highlight.php - Postprocess search results before they are returned by the query.
File
- src/
Plugin/ search_api/ processor/ Highlight.php, line 315
Class
- Highlight
- Adds a highlighted excerpt to results and highlights returned fields.
Namespace
Drupal\search_api\Plugin\search_api\processorCode
protected function highlightFields(array $results, array $keys) {
$highlighted_fields = [];
foreach ($results as $item_id => $item) {
// Maybe the backend or some other processor has already set highlighted
// field values.
$highlighted_fields[$item_id] = $item
->getExtraData('highlighted_fields', []);
}
$load = $this->configuration['highlight'] == 'always';
$item_fields = $this
->getFulltextFields($results, NULL, $load);
foreach ($item_fields as $item_id => $fields) {
foreach ($fields as $field_id => $values) {
if (empty($highlighted_fields[$item_id][$field_id])) {
$change = FALSE;
foreach ($values as $i => $value) {
$values[$i] = $this
->highlightField($value, $keys);
if ($values[$i] !== $value) {
$change = TRUE;
}
}
if ($change) {
$highlighted_fields[$item_id][$field_id] = $values;
}
}
}
}
return $highlighted_fields;
}