3 namespace Drupal\help\Plugin\Block;
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;
14 * Provides a 'Help' block.
18 * admin_label = @Translation("Help")
21 class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface {
26 * @var \Drupal\Core\Extension\ModuleHandlerInterface
28 protected $moduleHandler;
31 * The current request.
33 * @var \Symfony\Component\HttpFoundation\Request
38 * The current route match.
40 * @var \Drupal\Core\Routing\RouteMatchInterface
42 protected $routeMatch;
45 * Creates a HelpBlock instance.
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
57 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
58 * The current route match.
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);
63 $this->request = $request;
64 $this->moduleHandler = $module_handler;
65 $this->routeMatch = $route_match;
71 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
76 $container->get('request_stack')->getCurrentRequest(),
77 $container->get('module_handler'),
78 $container->get('current_route_match')
85 public function build() {
86 // Do not show on a 403 or 404 page.
87 if ($this->request->attributes->has('exception')) {
91 $implementations = $this->moduleHandler->getImplementations('help');
94 $this->routeMatch->getRouteName(),
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
102 $build[] = is_array($help) ? $help : ['#markup' => $help];
111 public function getCacheContexts() {
112 return Cache::mergeContexts(parent::getCacheContexts(), ['route']);