function NodeSearch::prepareResults
Same name in other branches
- 9 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::prepareResults()
- 8.9.x core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::prepareResults()
- 11.x 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 352
Class
- NodeSearch
- Handles searching for node entities using the Search module index.
Namespace
Drupal\node\Plugin\SearchCode
protected function prepareResults(StatementInterface $found) {
$results = [];
$node_storage = $this->entityTypeManager
->getStorage('node');
$node_render = $this->entityTypeManager
->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->entityTypeManager
->getStorage('node_type')
->load($node->bundle());
unset($build['#theme']);
$build['#pre_render'][] = [
$this,
'removeSubmittedInfo',
];
// Fetch comments for snippet.
$rendered = $this->renderer
->renderInIsolation($build);
$this->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
$rendered .= ' ' . $this->moduleHandler
->invoke('comment', 'node_update_index', [
$node,
]);
$extra = $this->moduleHandler
->invokeAll('node_search_result', [
$node,
]);
$username = [
'#theme' => 'username',
'#account' => $node->getOwner(),
];
$result = [
'link' => $node->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(),
'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 += [
'user' => $this->renderer
->renderInIsolation($username),
'date' => $node->getChangedTime(),
];
}
$results[] = $result;
}
return $results;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.