You are here

protected function HelpSearch::prepareResults in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::prepareResults()

Prepares search results for display.

Parameters

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

Return value

array List of search result render arrays, with links, snippets, etc.

1 call to HelpSearch::prepareResults()
HelpSearch::execute in core/modules/help_topics/src/Plugin/Search/HelpSearch.php
Executes the search.

File

core/modules/help_topics/src/Plugin/Search/HelpSearch.php, line 275

Class

HelpSearch
Handles searching for help using the Search module index.

Namespace

Drupal\help_topics\Plugin\Search

Code

protected function prepareResults(StatementInterface $found) {
  $results = [];
  $plugins = [];
  $languages = [];
  $keys = $this
    ->getKeywords();
  foreach ($found as $item) {
    $section_plugin_id = $item->section_plugin_id;
    if (!isset($plugins[$section_plugin_id])) {
      $plugins[$section_plugin_id] = $this
        ->getSectionPlugin($section_plugin_id);
    }
    if ($plugins[$section_plugin_id]) {
      $langcode = $item->langcode;
      if (!isset($languages[$langcode])) {
        $languages[$langcode] = $this->languageManager
          ->getLanguage($item->langcode);
      }
      $topic = $plugins[$section_plugin_id]
        ->renderTopicForSearch($item->topic_id, $languages[$langcode]);
      if ($topic) {
        if (isset($topic['cacheable_metadata'])) {
          $this
            ->addCacheableDependency($topic['cacheable_metadata']);
        }
        $results[] = [
          'title' => $topic['title'],
          'link' => $topic['url']
            ->toString(),
          'snippet' => search_excerpt($keys, $topic['title'] . ' ' . $topic['text'], $item->langcode),
          'langcode' => $item->langcode,
        ];
      }
    }
  }
  return $results;
}