function HelpTwigExtension::getTopicLink

Same name in other branches
  1. 9 core/modules/help_topics/src/HelpTwigExtension.php \Drupal\help_topics\HelpTwigExtension::getTopicLink()
  2. 11.x core/modules/help/src/HelpTwigExtension.php \Drupal\help\HelpTwigExtension::getTopicLink()

Returns a link to a help topic, or the title of the topic.

Parameters

string $topic_id: The help topic ID.

Return value

array A render array with a generated absolute link to the given topic. If the user does not have permission to view the topic, or an exception occurs, such as the topic not being defined due to a module not being installed, a default string is returned.

See also

\Drupal\Core\Template\TwigExtension::getUrl()

File

core/modules/help/src/HelpTwigExtension.php, line 122

Class

HelpTwigExtension
Defines and registers Drupal Twig extensions for rendering help topics.

Namespace

Drupal\help

Code

public function getTopicLink(string $topic_id) : array {
    assert($this->pluginManager instanceof HelpTopicPluginManagerInterface, "The plugin manager hasn't been set up. Any configuration YAML file with a service directive dealing with the Twig configuration can cause this, most likely found in a recently installed or changed module.");
    $bubbles = new BubbleableMetadata();
    $bubbles->addCacheableDependency($this->pluginManager);
    try {
        $plugin = $this->pluginManager
            ->createInstance($topic_id);
    } catch (PluginNotFoundException $e) {
        // Not a topic.
        $plugin = FALSE;
    }
    if ($plugin) {
        $parameters = [
            'id' => $topic_id,
        ];
        $route = 'help.help_topic';
        $build = $this->getRouteLink($plugin->getLabel(), $route, $parameters);
        $bubbles->addCacheableDependency($plugin);
    }
    else {
        $build = [
            '#markup' => $this->t('Missing help topic %topic', [
                '%topic' => $topic_id,
            ]),
        ];
    }
    $bubbles->applyTo($build);
    return $build;
}

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