You are here

public function SearchApiEntity::preRender in Search API 8

Runs before any fields are rendered.

This gives the handlers some time to set up before any handler has been rendered.

Parameters

\Drupal\views\ResultRow[]|\ArrayAccess $values: An array of all ResultRow objects returned from the query.

Overrides SearchApiFieldTrait::preRender

See also

\Drupal\views\Plugin\views\field\FieldHandlerInterface::preRender()

File

src/Plugin/views/field/SearchApiEntity.php, line 213

Class

SearchApiEntity
Handles the display of entity reference fields in Search API Views.

Namespace

Drupal\search_api\Plugin\views\field

Code

public function preRender(&$values) {
  parent::preRender($values);

  // The parent method will just have loaded the entity IDs. We now multi-load
  // the actual objects.
  $property_path = $this
    ->getCombinedPropertyPath();
  foreach ($values as $i => $row) {
    if (!empty($row->{$property_path})) {
      foreach ((array) $row->{$property_path} as $j => $value) {
        if (is_scalar($value)) {
          $to_load[$value][] = [
            $i,
            $j,
          ];
        }
      }
    }
  }
  if (empty($to_load)) {
    return;
  }
  $entities = $this
    ->getEntityTypeManager()
    ->getStorage($this
    ->getTargetEntityTypeId())
    ->loadMultiple(array_keys($to_load));
  $account = $this
    ->getQuery()
    ->getAccessAccount();
  foreach ($entities as $id => $entity) {
    $bundle = $entity
      ->bundle();
    $operation = $this
      ->getDisplayMethod($bundle) == 'label' ? 'view label' : 'view';
    if ($entity
      ->access($operation, $account)) {
      foreach ($to_load[$id] as list($i, $j)) {
        if ($entity
          ->access('view', $account)) {
          $langcode = $values[$i]->search_api_language;
          if ($entity instanceof TranslatableInterface && $entity
            ->hasTranslation($langcode)) {
            $entity = $entity
              ->getTranslation($langcode);
          }
          $values[$i]->{$property_path}[$j] = $entity;
        }
      }
    }
  }
}