annotate core/modules/help/src/Controller/HelpController.php @ 19:fa3358dc1485 tip

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