HelpBlock.php in Drupal 8
File
core/modules/help/src/Plugin/Block/HelpBlock.php
View source
<?php
namespace Drupal\help\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $moduleHandler;
protected $request;
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request, ModuleHandlerInterface $module_handler, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->request = $request;
$this->moduleHandler = $module_handler;
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('request_stack')
->getCurrentRequest(), $container
->get('module_handler'), $container
->get('current_route_match'));
}
public function build() {
if ($this->request->attributes
->has('exception')) {
return [];
}
$implementations = $this->moduleHandler
->getImplementations('help');
$build = [];
$args = [
$this->routeMatch
->getRouteName(),
$this->routeMatch,
];
foreach ($implementations as $module) {
if ($help = $this->moduleHandler
->invoke($module, 'help', $args)) {
$build[] = is_array($help) ? $help : [
'#markup' => $help,
];
}
}
return $build;
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'route',
]);
}
}