You are here

protected function SearchApiHighlight::getFulltextFields in Search API 7

Retrieves the fulltext data of a result.

Parameters

array $results: All results returned in the search, by reference.

int|string $i: The index in the results array of the result whose data should be returned.

array $fulltext_fields: The fulltext fields from which the excerpt should be created.

bool $load: TRUE if the item should be loaded if necessary, FALSE if only fields already returned in the results should be used.

Return value

array An array containing fulltext field names mapped to the text data contained in them for the given result.

1 call to SearchApiHighlight::getFulltextFields()
SearchApiHighlight::postprocessSearchResults in includes/processor_highlight.inc
Does nothing.

File

includes/processor_highlight.inc, line 201
Contains the SearchApiHighlight class.

Class

SearchApiHighlight
Processor for highlighting search results.

Code

protected function getFulltextFields(array &$results, $i, array $fulltext_fields, $load = TRUE) {
  global $language;
  $data = array();
  $result =& $results[$i];

  // Act as if $load is TRUE if we have a loaded item.
  $load |= !empty($result['entity']);
  $result += array(
    'fields' => array(),
  );

  // We only need detailed fields data if $load is TRUE.
  $fields = $load ? $this->index
    ->getFields() : array();
  $needs_extraction = array();
  $returned_fields = search_api_get_sanitized_field_values(array_intersect_key($result['fields'], array_flip($fulltext_fields)));
  foreach ($fulltext_fields as $field) {
    if (array_key_exists($field, $returned_fields)) {
      $data[$field] = $returned_fields[$field];
    }
    elseif ($load) {
      $needs_extraction[$field] = $fields[$field];
    }
  }
  if (!$needs_extraction) {
    return $data;
  }
  if (empty($result['entity'])) {
    $items = $this->index
      ->loadItems(array_keys($results));
    foreach ($items as $id => $item) {
      $results[$id]['entity'] = $item;
    }
  }

  // If we still don't have a loaded item, we should stop trying.
  if (empty($result['entity'])) {
    return $data;
  }
  $wrapper = $this->index
    ->entityWrapper($result['entity'], FALSE);
  $wrapper
    ->language($language->language);
  $extracted = search_api_extract_fields($wrapper, $needs_extraction, array(
    'sanitize' => TRUE,
  ));
  foreach ($extracted as $field => $info) {
    if (isset($info['value'])) {
      $data[$field] = $info['value'];
    }
  }
  return $data;
}