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"),
20 * "settings_tray" = FALSE,
24 class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface {
29 * @var \Drupal\Core\Extension\ModuleHandlerInterface
31 protected $moduleHandler;
34 * The current request.
36 * @var \Symfony\Component\HttpFoundation\Request
41 * The current route match.
43 * @var \Drupal\Core\Routing\RouteMatchInterface
45 protected $routeMatch;
48 * Creates a HelpBlock instance.
50 * @param array $configuration
51 * A configuration array containing information about the plugin instance.
52 * @param string $plugin_id
53 * The plugin_id for the plugin instance.
54 * @param mixed $plugin_definition
55 * The plugin implementation definition.
56 * @param \Symfony\Component\HttpFoundation\Request $request
57 * The current request.
58 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
60 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
61 * The current route match.
63 public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request, ModuleHandlerInterface $module_handler, RouteMatchInterface $route_match) {
64 parent::__construct($configuration, $plugin_id, $plugin_definition);
66 $this->request = $request;
67 $this->moduleHandler = $module_handler;
68 $this->routeMatch = $route_match;
74 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
79 $container->get('request_stack')->getCurrentRequest(),
80 $container->get('module_handler'),
81 $container->get('current_route_match')
88 public function build() {
89 // Do not show on a 403 or 404 page.
90 if ($this->request->attributes->has('exception')) {
94 $implementations = $this->moduleHandler->getImplementations('help');
97 $this->routeMatch->getRouteName(),
100 foreach ($implementations as $module) {
101 // Don't add empty strings to $build array.
102 if ($help = $this->moduleHandler->invoke($module, 'help', $args)) {
103 // Convert strings to #markup render arrays so that they will XSS admin
105 $build[] = is_array($help) ? $help : ['#markup' => $help];
114 public function getCacheContexts() {
115 return Cache::mergeContexts(parent::getCacheContexts(), ['route']);