You are here

protected function SearchApiFieldTrait::checkHighlighting in Search API 8

Replaces extracted property values with highlighted field values.

Parameters

\Drupal\views\ResultRow[] $values: The Views result rows for which highlighted field values should be added where applicable and possible.

string|null $datasource_id: The datasource ID of the property to extract (or NULL for datasource- independent properties).

string $property_path: The property path of the property to extract.

string $combined_property_path: The combined property path of the property for which to add data.

1 call to SearchApiFieldTrait::checkHighlighting()
SearchApiFieldTrait::preRender in src/Plugin/views/field/SearchApiFieldTrait.php
Runs before any fields are rendered.

File

src/Plugin/views/field/SearchApiFieldTrait.php, line 924

Class

SearchApiFieldTrait
Provides a trait to use for Search API Views field handlers.

Namespace

Drupal\search_api\Plugin\views\field

Code

protected function checkHighlighting(array $values, $datasource_id, $property_path, $combined_property_path) {

  // If using highlighting data wasn't enabled, we can skip all of this
  // anyways.
  if (empty($this->options['use_highlighting'])) {
    return;
  }

  // Since (currently) only fields can be highlighted, not arbitrary
  // properties, we needn't even bother if there are no matching fields.
  $fields = $this
    ->getFieldsHelper()
    ->filterForPropertyPath($this
    ->getIndex()
    ->getFields(), $datasource_id, $property_path);
  if (!$fields) {
    return;
  }
  foreach ($values as $row) {

    // We only want highlighting data if we even wanted (and, thus, extracted)
    // the property's values in the first place.
    if (!isset($row->{$combined_property_path})) {
      continue;
    }
    $highlighted_data = $row->_item
      ->getExtraData('highlighted_fields');
    if (!$highlighted_data) {
      continue;
    }
    $highlighted_data = array_intersect_key($highlighted_data, $fields);
    if ($highlighted_data) {

      // There might be multiple fields with highlight data here, in rare
      // cases, but it's unclear how to combine them, or choose one over the
      // other, anyways, so just take the first one.
      $values = reset($highlighted_data);
      $values = $this
        ->combineHighlightedValues($row->{$combined_property_path}, $values);
      $row->{$combined_property_path} = $values;
    }
  }
}