04f8bcbab6e10c55f852707eaba5623296bafc37
[yaffs-website] / web / core / modules / help / src / Plugin / Block / HelpBlock.php
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  *   forms = {
20  *     "settings_tray" = FALSE,
21  *   },
22  * )
23  */
24 class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface {
25
26   /**
27    * The module handler.
28    *
29    * @var \Drupal\Core\Extension\ModuleHandlerInterface
30    */
31   protected $moduleHandler;
32
33   /**
34    * The current request.
35    *
36    * @var \Symfony\Component\HttpFoundation\Request
37    */
38   protected $request;
39
40   /**
41    * The current route match.
42    *
43    * @var \Drupal\Core\Routing\RouteMatchInterface
44    */
45   protected $routeMatch;
46
47   /**
48    * Creates a HelpBlock instance.
49    *
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
59    *   The module handler.
60    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
61    *   The current route match.
62    */
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);
65
66     $this->request = $request;
67     $this->moduleHandler = $module_handler;
68     $this->routeMatch = $route_match;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
75     return new static(
76       $configuration,
77       $plugin_id,
78       $plugin_definition,
79       $container->get('request_stack')->getCurrentRequest(),
80       $container->get('module_handler'),
81       $container->get('current_route_match')
82     );
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function build() {
89     // Do not show on a 403 or 404 page.
90     if ($this->request->attributes->has('exception')) {
91       return [];
92     }
93
94     $implementations = $this->moduleHandler->getImplementations('help');
95     $build = [];
96     $args = [
97       $this->routeMatch->getRouteName(),
98       $this->routeMatch,
99     ];
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
104         // filtered.
105         $build[] = is_array($help) ? $help : ['#markup' => $help];
106       }
107     }
108     return $build;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function getCacheContexts() {
115     return Cache::mergeContexts(parent::getCacheContexts(), ['route']);
116   }
117
118 }