You are here

public function HelpTwigExtension::getTopicLink in Drupal 10

Same name and namespace in other branches
  1. 9 core/modules/help_topics/src/HelpTwigExtension.php \Drupal\help_topics\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_topics/src/HelpTwigExtension.php, line 135

Class

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

Namespace

Drupal\help_topics

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