0ccf354e708976afe016b3b85ff250cfc38a9c10
[yaffs-website] / web / core / modules / help / src / Controller / HelpController.php
1 <?php
2
3 namespace Drupal\help\Controller;
4
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;
11
12 /**
13  * Controller routines for help routes.
14  */
15 class HelpController extends ControllerBase {
16
17   /**
18    * The current route match.
19    *
20    * @var \Drupal\Core\Routing\RouteMatchInterface
21    */
22   protected $routeMatch;
23
24   /**
25    * The help section plugin manager.
26    *
27    * @var \Drupal\help\HelpSectionManager
28    */
29   protected $helpManager;
30
31   /**
32    * Creates a new HelpController.
33    *
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.
38    */
39   public function __construct(RouteMatchInterface $route_match, HelpSectionManager $help_manager) {
40     $this->routeMatch = $route_match;
41     $this->helpManager = $help_manager;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container) {
48     return new static(
49       $container->get('current_route_match'),
50       $container->get('plugin.manager.help_section')
51     );
52   }
53
54   /**
55    * Prints a page listing various types of help.
56    *
57    * The page has sections defined by \Drupal\help\HelpSectionPluginInterface
58    * plugins.
59    *
60    * @return array
61    *   A render array for the help page.
62    */
63   public function helpMain() {
64     $output = [];
65
66     // We are checking permissions, so add the user.permissions cache context.
67     $cacheability = new CacheableMetadata();
68     $cacheability->addCacheContexts(['user.permissions']);
69
70     $plugins = $this->helpManager->getDefinitions();
71     $cacheability->addCacheableDependency($this->helpManager);
72
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'])) {
76         continue;
77       }
78
79       // Add the section to the page.
80       /** @var \Drupal\help\HelpSectionPluginInterface $plugin */
81       $plugin = $this->helpManager->createInstance($plugin_id);
82       $this_output = [
83         '#theme' => 'help_section',
84         '#title' => $plugin->getTitle(),
85         '#description' => $plugin->getDescription(),
86         '#empty' => $this->t('There is currently nothing in this section.'),
87         '#links' => [],
88       ];
89
90       $links = $plugin->listTopics();
91       if (is_array($links) && count($links)) {
92         $this_output['#links'] = $links;
93       }
94
95       $cacheability->addCacheableDependency($plugin);
96       $output[$plugin_id] = $this_output;
97     }
98
99     $cacheability->applyTo($output);
100     return $output;
101   }
102
103   /**
104    * Prints a page listing general help for a module.
105    *
106    * @param string $name
107    *   A module name to display a help page for.
108    *
109    * @return array
110    *   A render array as expected by
111    *   \Drupal\Core\Render\RendererInterface::render().
112    *
113    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
114    */
115   public function helpPage($name) {
116     $build = [];
117     if ($this->moduleHandler()->implementsHook($name, 'help')) {
118       $module_name = $this->moduleHandler()->getName($name);
119       $build['#title'] = $module_name;
120
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']));
124       }
125
126       $temp = $this->moduleHandler()->invoke($name, 'help', ["help.page.$name", $this->routeMatch]);
127       if (empty($temp)) {
128         $build['top'] = ['#markup' => $this->t('No help is available for module %module.', ['%module' => $module_name])];
129       }
130       else {
131         if (!is_array($temp)) {
132           $temp = ['#markup' => $temp];
133         }
134         $build['top'] = $temp;
135       }
136
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)) {
141         $links = [];
142         foreach ($admin_tasks as $task) {
143           $link['url'] = $task['url'];
144           $link['title'] = $task['title'];
145           $links[] = $link;
146         }
147         $build['links'] = [
148           '#theme' => 'links__help',
149           '#heading' => [
150             'level' => 'h3',
151             'text' => $this->t('@module administration pages', ['@module' => $module_name]),
152           ],
153           '#links' => $links,
154         ];
155       }
156       return $build;
157     }
158     else {
159       throw new NotFoundHttpException();
160     }
161   }
162
163 }