Mercurial > hg > isophonics-drupal-site
comparison core/modules/help/src/Plugin/Block/HelpBlock.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\help\Plugin\Block; | |
4 | |
5 use Drupal\Core\Block\BlockBase; | |
6 use Drupal\Core\Cache\Cache; | |
7 use Drupal\Core\Extension\ModuleHandlerInterface; | |
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
9 use Drupal\Core\Routing\RouteMatchInterface; | |
10 use Symfony\Component\DependencyInjection\ContainerInterface; | |
11 use Symfony\Component\HttpFoundation\Request; | |
12 | |
13 /** | |
14 * Provides a 'Help' block. | |
15 * | |
16 * @Block( | |
17 * id = "help_block", | |
18 * admin_label = @Translation("Help") | |
19 * ) | |
20 */ | |
21 class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface { | |
22 | |
23 /** | |
24 * The module handler. | |
25 * | |
26 * @var \Drupal\Core\Extension\ModuleHandlerInterface | |
27 */ | |
28 protected $moduleHandler; | |
29 | |
30 /** | |
31 * The current request. | |
32 * | |
33 * @var \Symfony\Component\HttpFoundation\Request | |
34 */ | |
35 protected $request; | |
36 | |
37 /** | |
38 * The current route match. | |
39 * | |
40 * @var \Drupal\Core\Routing\RouteMatchInterface | |
41 */ | |
42 protected $routeMatch; | |
43 | |
44 /** | |
45 * Creates a HelpBlock instance. | |
46 * | |
47 * @param array $configuration | |
48 * A configuration array containing information about the plugin instance. | |
49 * @param string $plugin_id | |
50 * The plugin_id for the plugin instance. | |
51 * @param mixed $plugin_definition | |
52 * The plugin implementation definition. | |
53 * @param \Symfony\Component\HttpFoundation\Request $request | |
54 * The current request. | |
55 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | |
56 * The module handler. | |
57 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match | |
58 * The current route match. | |
59 */ | |
60 public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request, ModuleHandlerInterface $module_handler, RouteMatchInterface $route_match) { | |
61 parent::__construct($configuration, $plugin_id, $plugin_definition); | |
62 | |
63 $this->request = $request; | |
64 $this->moduleHandler = $module_handler; | |
65 $this->routeMatch = $route_match; | |
66 } | |
67 | |
68 /** | |
69 * {@inheritdoc} | |
70 */ | |
71 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
72 return new static( | |
73 $configuration, | |
74 $plugin_id, | |
75 $plugin_definition, | |
76 $container->get('request_stack')->getCurrentRequest(), | |
77 $container->get('module_handler'), | |
78 $container->get('current_route_match') | |
79 ); | |
80 } | |
81 | |
82 /** | |
83 * {@inheritdoc} | |
84 */ | |
85 public function build() { | |
86 // Do not show on a 403 or 404 page. | |
87 if ($this->request->attributes->has('exception')) { | |
88 return []; | |
89 } | |
90 | |
91 $implementations = $this->moduleHandler->getImplementations('help'); | |
92 $build = []; | |
93 $args = [ | |
94 $this->routeMatch->getRouteName(), | |
95 $this->routeMatch, | |
96 ]; | |
97 foreach ($implementations as $module) { | |
98 // Don't add empty strings to $build array. | |
99 if ($help = $this->moduleHandler->invoke($module, 'help', $args)) { | |
100 // Convert strings to #markup render arrays so that they will XSS admin | |
101 // filtered. | |
102 $build[] = is_array($help) ? $help : ['#markup' => $help]; | |
103 } | |
104 } | |
105 return $build; | |
106 } | |
107 | |
108 /** | |
109 * {@inheritdoc} | |
110 */ | |
111 public function getCacheContexts() { | |
112 return Cache::mergeContexts(parent::getCacheContexts(), ['route']); | |
113 } | |
114 | |
115 } |