function HelpSearch::prepareResults

Same name in other branches
  1. 9 core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::prepareResults()
  2. 10 core/modules/help/src/Plugin/Search/HelpSearch.php \Drupal\help\Plugin\Search\HelpSearch::prepareResults()
  3. 11.x core/modules/help/src/Plugin/Search/HelpSearch.php \Drupal\help\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 270

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.