You are here

protected function SearchApiQuery::addResults in Search API 8

Adds Search API result items to a view's result set.

Parameters

\Drupal\search_api\Query\ResultSetInterface $result_set: The search results.

\Drupal\views\ViewExecutable $view: The executed view.

1 call to SearchApiQuery::addResults()
SearchApiQuery::execute in src/Plugin/views/query/SearchApiQuery.php
Executes the query and fills the associated view object with according values.

File

src/Plugin/views/query/SearchApiQuery.php, line 647

Class

SearchApiQuery
Defines a Views query class for searching on Search API indexes.

Namespace

Drupal\search_api\Plugin\views\query

Code

protected function addResults(ResultSetInterface $result_set, ViewExecutable $view) {
  $results = $result_set
    ->getResultItems();

  // Views \Drupal\views\Plugin\views\style\StylePluginBase::renderFields()
  // uses a numeric results index to key the rendered results.
  // The ResultRow::index property is the key then used to retrieve these.
  $count = 0;

  // First, unless disabled, check access for all entities in the results.
  if (!$this->options['skip_access']) {
    $account = $this
      ->getAccessAccount();

    // If search items are not loaded already, pre-load them now in bulk to
    // avoid them being individually loaded inside checkAccess().
    $result_set
      ->preLoadResultItems();
    foreach ($results as $item_id => $result) {
      if (!$result
        ->getAccessResult($account)
        ->isAllowed()) {
        unset($results[$item_id]);
      }
    }
  }
  foreach ($results as $item_id => $result) {
    $values = [];
    $values['_item'] = $result;
    try {
      $object = $result
        ->getOriginalObject(FALSE);
      if ($object) {
        $values['_object'] = $object;
        $values['_relationship_objects'][NULL] = [
          $object,
        ];
        if ($object instanceof EntityAdapter) {
          $values['_entity'] = $object
            ->getEntity();
        }
      }
    } catch (SearchApiException $e) {

      // Can't actually be thrown here, but catch for the static analyzer's
      // sake.
    }

    // Gather any properties from the search results.
    foreach ($result
      ->getFields(FALSE) as $field_id => $field) {
      if ($field
        ->getValues()) {
        $path = $field
          ->getCombinedPropertyPath();
        try {
          $property = $field
            ->getDataDefinition();

          // For configurable processor-defined properties, our Views field
          // handlers use a special property path to distinguish multiple
          // fields with the same property path. Therefore, we here also set
          // the values using that special property path so this will work
          // correctly.
          if ($property instanceof ConfigurablePropertyInterface) {
            $path .= '|' . $field_id;
          }
        } catch (SearchApiException $e) {

          // If we're not able to retrieve the data definition at this point,
          // it doesn't really matter.
        }
        $values[$path] = $field
          ->getValues();
      }
    }
    $values['index'] = $count++;
    $view->result[] = new ResultRow($values);
  }
}