You are here

protected function SearchApiPageController::createItemRenderArray in Search API Pages 8

Creates a render array for the given result item.

Parameters

\Drupal\search_api\Item\ItemInterface $item: The item to render.

\Drupal\search_api_page\SearchApiPageInterface $search_api_page: The search api page.

Return value

array A render array for the given result item.

1 call to SearchApiPageController::createItemRenderArray()
SearchApiPageController::page in src/Controller/SearchApiPageController.php
Page callback.

File

src/Controller/SearchApiPageController.php, line 157

Class

SearchApiPageController
Defines a controller to serve search pages.

Namespace

Drupal\search_api_page\Controller

Code

protected function createItemRenderArray(ItemInterface $item, SearchApiPageInterface $search_api_page) {
  try {
    $originalObject = $item
      ->getOriginalObject();
    if ($originalObject === NULL) {
      return [];
    }

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $originalObject
      ->getValue();
  } catch (SearchApiException $e) {
    return [];
  }
  if (!$entity) {
    return [];
  }
  $viewedResult = [];
  if ($search_api_page
    ->renderAsViewModes()) {
    $datasource_id = 'entity:' . $entity
      ->getEntityTypeId();
    $bundle = $entity
      ->bundle();
    $viewMode = $search_api_page
      ->getViewModeConfig()
      ->getViewMode($datasource_id, $bundle);
    $viewedResult = $this
      ->entityTypeManager()
      ->getViewBuilder($entity
      ->getEntityTypeId())
      ->view($entity, $viewMode);
    $viewedResult['#search_api_excerpt'] = $item
      ->getExcerpt();
  }
  if ($search_api_page
    ->renderAsSnippets()) {
    $viewedResult = [
      '#theme' => 'search_api_page_result',
      '#item' => $item,
      '#entity' => $entity,
    ];
  }
  $metadata = CacheableMetadata::createFromRenderArray($viewedResult);
  $metadata
    ->addCacheContexts([
    'url.path',
  ]);
  $metadata
    ->addCacheTags([
    'search_api_page.style',
  ]);
  $metadata
    ->applyTo($viewedResult);
  return $viewedResult;
}