You are here

protected function PhotosImageSearch::prepareResults in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/Search/PhotosImageSearch.php \Drupal\photos\Plugin\Search\PhotosImageSearch::prepareResults()

Prepares search results for rendering.

Parameters

\Drupal\Core\Database\StatementInterface $found: Results found from a successful search query execute() method.

Return value

array Array of search result item render arrays (empty array if no results).

1 call to PhotosImageSearch::prepareResults()
PhotosImageSearch::execute in src/Plugin/Search/PhotosImageSearch.php
Executes the search.

File

src/Plugin/Search/PhotosImageSearch.php, line 359

Class

PhotosImageSearch
Handles searching for photos_image entities using the Search module index.

Namespace

Drupal\photos\Plugin\Search

Code

protected function prepareResults(StatementInterface $found) {
  $results = [];
  $nodeStorage = $this->entityTypeManager
    ->getStorage('node');
  $nodeRender = $this->entityTypeManager
    ->getViewBuilder('node');
  $photosImageStorage = $this->entityTypeManager
    ->getStorage('photos_image');
  $photosImageRender = $this->entityTypeManager
    ->getViewBuilder('photos_image');
  $keys = $this->keywords;
  foreach ($found as $item) {

    // Render the node.

    /** @var \Drupal\photos\Entity\PhotosImage $photosImage */
    $photosImage = $photosImageStorage
      ->load($item->sid)
      ->getTranslation($item->langcode);
    $build = $photosImageRender
      ->view($photosImage, 'search_result', $item->langcode);

    /** @var \Drupal\node\NodeInterface $node */
    $node = $nodeStorage
      ->load($photosImage
      ->getAlbumId())
      ->getTranslation($item->langcode);
    unset($build['#theme']);
    $build['#pre_render'][] = [
      $this,
      'removeSubmittedInfo',
    ];

    // Fetch comments for snippet.
    $rendered = $this->renderer
      ->renderPlain($build);
    $this
      ->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
    $rendered .= ' ' . $this->moduleHandler
      ->invoke('comment', 'photos_image_update_index', [
      $photosImage,
    ]);
    $extra = $this->moduleHandler
      ->invokeAll('photos_image_search_result', [
      $photosImage,
    ]);
    $username = [
      '#theme' => 'username',
      '#account' => $photosImage
        ->getOwner(),
    ];
    $result = [
      'link' => $photosImage
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString(),
      'album_title' => $node
        ->label(),
      'title' => $photosImage
        ->label(),
      'node' => $node,
      'photos_image' => $photosImage,
      'extra' => $extra,
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, $rendered, $item->langcode),
      'langcode' => $photosImage
        ->language()
        ->getId(),
    ];
    $this
      ->addCacheableDependency($photosImage);

    // We have to separately add the photos_image owner's cache tags because
    // search module doesn't use the rendering system, it does its own
    // rendering without taking cacheability metadata into account. So we have
    // to do it explicitly here.
    $this
      ->addCacheableDependency($photosImage
      ->getOwner());
    $result += [
      'user' => $this->renderer
        ->renderPlain($username),
      'date' => $photosImage
        ->getChangedTime(),
    ];
    $results[] = $result;
  }
  return $results;
}