You are here

protected function SearchApiFieldTrait::preLoadResultItems in Search API 8

Pre-loads the result objects, where necessary.

Parameters

\Drupal\views\ResultRow[] $values: The Views result rows for which result objects should be loaded.

string[] $dependents: The actually required properties (as combined property paths) that depend on the result objects.

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

File

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

Class

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

Namespace

Drupal\search_api\Plugin\views\field

Code

protected function preLoadResultItems(array $values, array $dependents) {
  $to_load = [];
  foreach ($values as $i => $row) {

    // If the object is already set on the result row, we've got nothing to do
    // here.
    if (!empty($row->_object)) {
      continue;
    }

    // Same if the object was loaded on the result item already.
    $object = $row->_item
      ->getOriginalObject(FALSE);
    if ($object) {
      $row->_object = $object;
      $row->_relationship_objects[NULL] = [
        $object,
      ];
      continue;
    }

    // We also don't need to load the object if all field values that depend
    // on it are already present on the result row.
    $required = FALSE;
    foreach ($dependents as $dependent) {
      if (!isset($row->{$dependent})) {
        $required = TRUE;
        break;
      }
    }
    if (!$required) {
      continue;
    }
    $to_load[$row->search_api_id] = $i;
  }
  if (!$to_load) {
    return;
  }
  $items = $this
    ->getIndex()
    ->loadItemsMultiple(array_keys($to_load));
  foreach ($to_load as $item_id => $i) {
    if (!empty($items[$item_id])) {
      $values[$i]->_object = $items[$item_id];
      $values[$i]->_relationship_objects[NULL] = [
        $items[$item_id],
      ];
    }
  }
}