function HelpTopicSection::renderTopicForSearch

Same name in other branches
  1. 9 core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php \Drupal\help_topics\Plugin\HelpSection\HelpTopicSection::renderTopicForSearch()
  2. 8.9.x core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php \Drupal\help_topics\Plugin\HelpSection\HelpTopicSection::renderTopicForSearch()
  3. 10 core/modules/help/src/Plugin/HelpSection/HelpTopicSection.php \Drupal\help\Plugin\HelpSection\HelpTopicSection::renderTopicForSearch()

Overrides SearchableHelpInterface::renderTopicForSearch

File

core/modules/help/src/Plugin/HelpSection/HelpTopicSection.php, line 163

Class

HelpTopicSection
Provides the help topics list section for the help page.

Namespace

Drupal\help\Plugin\HelpSection

Code

public function renderTopicForSearch($topic_id, LanguageInterface $language) {
    $plugin = $this->pluginManager
        ->createInstance($topic_id);
    if (!$plugin) {
        return [];
    }
    // We are rendering this topic for search indexing or search results,
    // possibly in a different language than the current language. The topic
    // title and body come from translatable things in the Twig template, so we
    // need to set the default language to the desired language, render them,
    // then restore the default language so we do not affect other cron
    // processes. Also, just in case there is an exception, wrap the whole
    // thing in a try/finally block, and reset the language in the finally part.
    $old_language = $this->defaultLanguage
        ->get();
    try {
        if ($old_language->getId() !== $language->getId()) {
            $this->defaultLanguage
                ->set($language);
            $this->translationManager
                ->setDefaultLangcode($language->getId());
            $this->languageManager
                ->reset();
        }
        $topic = [];
        // Render the title in this language.
        $title_build = [
            'title' => [
                '#type' => '#markup',
                '#markup' => $plugin->getLabel(),
            ],
        ];
        $topic['title'] = $this->renderer
            ->renderInIsolation($title_build);
        $cacheable_metadata = CacheableMetadata::createFromRenderArray($title_build);
        // Render the body in this language. For this, we need to set up a render
        // context, because the Twig plugins that provide the body assumes one
        // is present.
        $context = new RenderContext();
        $build = [
            'body' => $this->renderer
                ->executeInRenderContext($context, [
                $plugin,
                'getBody',
            ]),
        ];
        $topic['text'] = $this->renderer
            ->renderInIsolation($build);
        $cacheable_metadata->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
        $cacheable_metadata->addCacheableDependency($plugin);
        if (!$context->isEmpty()) {
            $cacheable_metadata->addCacheableDependency($context->pop());
        }
        // Add the other information.
        $topic['url'] = $plugin->toUrl();
        $topic['cacheable_metadata'] = $cacheable_metadata;
    } finally {
        // Restore the original language.
        if ($old_language->getId() !== $language->getId()) {
            $this->defaultLanguage
                ->set($old_language);
            $this->translationManager
                ->setDefaultLangcode($old_language->getId());
            $this->languageManager
                ->reset();
        }
    }
    return $topic;
}

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