HelpTopicPluginController.php in Drupal 9
File
core/modules/help_topics/src/Controller/HelpTopicPluginController.php
View source
<?php
namespace Drupal\help_topics\Controller;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\help_topics\HelpTopicPluginManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class HelpTopicPluginController extends ControllerBase {
protected $renderer;
protected $helpTopicPluginManager;
public function __construct(HelpTopicPluginManagerInterface $help_topic_plugin_manager, RendererInterface $renderer) {
$this->helpTopicPluginManager = $help_topic_plugin_manager;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.help_topic'), $container
->get('renderer'));
}
public function viewHelpTopic($id) {
$build = [];
if (!$this->helpTopicPluginManager
->hasDefinition($id)) {
throw new NotFoundHttpException();
}
$help_topic = $this->helpTopicPluginManager
->createInstance($id);
$build['#body'] = $help_topic
->getBody();
$this->renderer
->addCacheableDependency($build, $help_topic);
$links = [];
$related = $help_topic
->getRelated();
foreach ($related as $other_id) {
if ($other_id !== $id) {
$topic = $this->helpTopicPluginManager
->createInstance($other_id);
$links[$other_id] = [
'title' => $topic
->getLabel(),
'url' => Url::fromRoute('help.help_topic', [
'id' => $other_id,
]),
];
$this->renderer
->addCacheableDependency($build, $topic);
}
}
if (count($links)) {
uasort($links, [
SortArray::class,
'sortByTitleElement',
]);
$build['#related'] = [
'#theme' => 'links__related',
'#heading' => [
'text' => $this
->t('Related topics'),
'level' => 'h2',
],
'#links' => $links,
];
}
$build['#theme'] = 'help_topic';
$build['#title'] = $help_topic
->getLabel();
return $build;
}
}