You are here

protected function SearchApiMultiViewsQuery::addResults in Search API Multi-Index Searches 7

Helper function for adding results to a view in the format expected by the view.

Overrides SearchApiViewsQuery::addResults

File

views/query.inc, line 72

Class

SearchApiMultiViewsQuery
Views query class using a Search API index as the data source.

Code

protected function addResults(array $results, $view) {
  $indexes = $this
    ->getIndexes();
  foreach ($results as $result) {
    $row = array();
    if (!empty($this->options['entity_access']) && isset($indexes[$result['index_id']]) && $indexes[$result['index_id']]
      ->getEntityType()) {
      $entity = $indexes[$result['index_id']]
        ->loadItems(array(
        $result['id'],
      ));
      if (!$entity || !entity_access('view', $indexes[$result['index_id']]->item_type, reset($entity))) {
        continue;
      }
    }

    // Include the loaded item for this result row, if present, or the item
    // ID.
    if (!empty($result['entity'])) {
      $row['entity'] = $result['entity'];
    }
    else {
      $row['entity'] = $result['id'];
    }

    // Gather any fields from the search results.
    if (!empty($result['fields'])) {
      foreach (search_api_get_sanitized_field_values($result['fields']) as $field_id => $value) {
        $field_id = $result['index_id'] . ':' . $field_id;
        $row['_entity_properties'][$field_id] = $value;
      }
    }
    $row['_entity_properties']['search_api_multi_id'] = $result['id'];
    $row['_entity_properties']['search_api_multi_index'] = $result['index_id'];
    $row['_entity_properties']['search_api_relevance'] = $result['score'];
    $row['_entity_properties']['search_api_excerpt'] = empty($result['excerpt']) ? '' : $result['excerpt'];

    // Add the row to the Views result set.
    $view->result[] = (object) $row;
  }
}