View source
<?php
namespace Drupal\help_topics;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class HelpTwigExtension extends AbstractExtension {
use StringTranslationTrait;
protected $accessManager;
protected $pluginManager;
public function __construct(AccessManagerInterface $access_manager, HelpTopicPluginManagerInterface $plugin_manager, TranslationInterface $string_translation) {
$this->accessManager = $access_manager;
$this->pluginManager = $plugin_manager;
$this->stringTranslation = $string_translation;
}
public function getFunctions() {
return [
new TwigFunction('help_route_link', [
$this,
'getRouteLink',
]),
new TwigFunction('help_topic_link', [
$this,
'getTopicLink',
]),
];
}
public function getRouteLink(string $text, string $route, array $parameters = [], array $options = []) : array {
assert($this->accessManager instanceof AccessManagerInterface, "The access 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
->addCacheTags([
'route_match',
]);
try {
$access_object = $this->accessManager
->checkNamedRoute($route, $parameters, NULL, TRUE);
$bubbles
->addCacheableDependency($access_object);
if ($access_object
->isAllowed()) {
$options['absolute'] = TRUE;
$url = Url::fromRoute($route, $parameters, $options);
$generated = $url
->toString(TRUE);
$bubbles
->addCacheableDependency($generated);
$build = [
'#title' => $text,
'#type' => 'link',
'#url' => $url,
];
}
else {
$build = [
'#markup' => $text,
];
}
} catch (RouteNotFoundException|MissingMandatoryParametersException|InvalidParameterException $e) {
$build = [
'#markup' => $text,
];
}
$bubbles
->applyTo($build);
return $build;
}
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) {
$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;
}
}