comparison core/modules/help/src/Controller/HelpController.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children c2387f117808
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 drupal_render().
111 *
112 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
113 */
114 public function helpPage($name) {
115 $build = [];
116 if ($this->moduleHandler()->implementsHook($name, 'help')) {
117 $module_name = $this->moduleHandler()->getName($name);
118 $build['#title'] = $module_name;
119
120 $info = system_get_info('module', $name);
121 if ($info['package'] === 'Core (Experimental)') {
122 drupal_set_message($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']), 'warning');
123 }
124
125 $temp = $this->moduleHandler()->invoke($name, 'help', ["help.page.$name", $this->routeMatch]);
126 if (empty($temp)) {
127 $build['top'] = ['#markup' => $this->t('No help is available for module %module.', ['%module' => $module_name])];
128 }
129 else {
130 if (!is_array($temp)) {
131 $temp = ['#markup' => $temp];
132 }
133 $build['top'] = $temp;
134 }
135
136 // Only print list of administration pages if the module in question has
137 // any such pages associated with it.
138 $admin_tasks = system_get_module_admin_tasks($name, system_get_info('module', $name));
139 if (!empty($admin_tasks)) {
140 $links = [];
141 foreach ($admin_tasks as $task) {
142 $link['url'] = $task['url'];
143 $link['title'] = $task['title'];
144 $links[] = $link;
145 }
146 $build['links'] = [
147 '#theme' => 'links__help',
148 '#heading' => [
149 'level' => 'h3',
150 'text' => $this->t('@module administration pages', ['@module' => $module_name]),
151 ],
152 '#links' => $links,
153 ];
154 }
155 return $build;
156 }
157 else {
158 throw new NotFoundHttpException();
159 }
160 }
161
162 }