You are here

protected function NodeSearch::prepareResults in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::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 NodeSearch::prepareResults()
NodeSearch::execute in core/modules/node/src/Plugin/Search/NodeSearch.php
Executes the search.

File

core/modules/node/src/Plugin/Search/NodeSearch.php, line 318
Contains \Drupal\node\Plugin\Search\NodeSearch.

Class

NodeSearch
Handles searching for node entities using the Search module index.

Namespace

Drupal\node\Plugin\Search

Code

protected function prepareResults(StatementInterface $found) {
  $results = array();
  $node_storage = $this->entityManager
    ->getStorage('node');
  $node_render = $this->entityManager
    ->getViewBuilder('node');
  $keys = $this->keywords;
  foreach ($found as $item) {

    // Render the node.

    /** @var \Drupal\node\NodeInterface $node */
    $node = $node_storage
      ->load($item->sid)
      ->getTranslation($item->langcode);
    $build = $node_render
      ->view($node, 'search_result', $item->langcode);

    /** @var \Drupal\node\NodeTypeInterface $type*/
    $type = $this->entityManager
      ->getStorage('node_type')
      ->load($node
      ->bundle());
    unset($build['#theme']);
    $build['#pre_render'][] = array(
      $this,
      'removeSubmittedInfo',
    );

    // Fetch comments for snippet.
    $rendered = $this->renderer
      ->renderPlain($build);
    $this
      ->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
    $rendered .= ' ' . $this->moduleHandler
      ->invoke('comment', 'node_update_index', [
      $node,
    ]);
    $extra = $this->moduleHandler
      ->invokeAll('node_search_result', [
      $node,
    ]);
    $language = $this->languageManager
      ->getLanguage($item->langcode);
    $username = array(
      '#theme' => 'username',
      '#account' => $node
        ->getOwner(),
    );
    $result = array(
      'link' => $node
        ->url('canonical', array(
        'absolute' => TRUE,
        'language' => $language,
      )),
      'type' => $type
        ->label(),
      'title' => $node
        ->label(),
      'node' => $node,
      'extra' => $extra,
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, $rendered, $item->langcode),
      'langcode' => $node
        ->language()
        ->getId(),
    );
    $this
      ->addCacheableDependency($node);

    // We have to separately add the node 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($node
      ->getOwner());
    if ($type
      ->displaySubmitted()) {
      $result += array(
        'user' => $this->renderer
          ->renderPlain($username),
        'date' => $node
          ->getChangedTime(),
      );
    }
    $results[] = $result;
  }
  return $results;
}