3 namespace Drupal\help\Controller;
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\help\HelpSectionManager;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13 * Controller routines for help routes.
15 class HelpController extends ControllerBase {
18 * The current route match.
20 * @var \Drupal\Core\Routing\RouteMatchInterface
22 protected $routeMatch;
25 * The help section plugin manager.
27 * @var \Drupal\help\HelpSectionManager
29 protected $helpManager;
32 * Creates a new HelpController.
34 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
35 * The current route match.
36 * @param \Drupal\help\HelpSectionManager $help_manager
37 * The help section manager.
39 public function __construct(RouteMatchInterface $route_match, HelpSectionManager $help_manager) {
40 $this->routeMatch = $route_match;
41 $this->helpManager = $help_manager;
47 public static function create(ContainerInterface $container) {
49 $container->get('current_route_match'),
50 $container->get('plugin.manager.help_section')
55 * Prints a page listing various types of help.
57 * The page has sections defined by \Drupal\help\HelpSectionPluginInterface
61 * A render array for the help page.
63 public function helpMain() {
66 // We are checking permissions, so add the user.permissions cache context.
67 $cacheability = new CacheableMetadata();
68 $cacheability->addCacheContexts(['user.permissions']);
70 $plugins = $this->helpManager->getDefinitions();
71 $cacheability->addCacheableDependency($this->helpManager);
73 foreach ($plugins as $plugin_id => $plugin_definition) {
74 // Check the provided permission.
75 if (!empty($plugin_definition['permission']) && !$this->currentuser()->hasPermission($plugin_definition['permission'])) {
79 // Add the section to the page.
80 /** @var \Drupal\help\HelpSectionPluginInterface $plugin */
81 $plugin = $this->helpManager->createInstance($plugin_id);
83 '#theme' => 'help_section',
84 '#title' => $plugin->getTitle(),
85 '#description' => $plugin->getDescription(),
86 '#empty' => $this->t('There is currently nothing in this section.'),
90 $links = $plugin->listTopics();
91 if (is_array($links) && count($links)) {
92 $this_output['#links'] = $links;
95 $cacheability->addCacheableDependency($plugin);
96 $output[$plugin_id] = $this_output;
99 $cacheability->applyTo($output);
104 * Prints a page listing general help for a module.
106 * @param string $name
107 * A module name to display a help page for.
110 * A render array as expected by
111 * \Drupal\Core\Render\RendererInterface::render().
113 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
115 public function helpPage($name) {
117 if ($this->moduleHandler()->implementsHook($name, 'help')) {
118 $module_name = $this->moduleHandler()->getName($name);
119 $build['#title'] = $module_name;
121 $info = system_get_info('module', $name);
122 if ($info['package'] === 'Core (Experimental)') {
123 $this->messenger()->addWarning($this->t('This module is experimental. <a href=":url">Experimental modules</a> are provided for testing purposes only. Use at your own risk.', [':url' => 'https://www.drupal.org/core/experimental']));
126 $temp = $this->moduleHandler()->invoke($name, 'help', ["help.page.$name", $this->routeMatch]);
128 $build['top'] = ['#markup' => $this->t('No help is available for module %module.', ['%module' => $module_name])];
131 if (!is_array($temp)) {
132 $temp = ['#markup' => $temp];
134 $build['top'] = $temp;
137 // Only print list of administration pages if the module in question has
138 // any such pages associated with it.
139 $admin_tasks = system_get_module_admin_tasks($name, system_get_info('module', $name));
140 if (!empty($admin_tasks)) {
142 foreach ($admin_tasks as $task) {
143 $link['url'] = $task['url'];
144 $link['title'] = $task['title'];
148 '#theme' => 'links__help',
151 'text' => $this->t('@module administration pages', ['@module' => $module_name]),
159 throw new NotFoundHttpException();