class HelpBlock

Same name in other branches
  1. 9 core/modules/help/src/Plugin/Block/HelpBlock.php \Drupal\help\Plugin\Block\HelpBlock
  2. 10 core/modules/help/src/Plugin/Block/HelpBlock.php \Drupal\help\Plugin\Block\HelpBlock
  3. 11.x core/modules/help/src/Plugin/Block/HelpBlock.php \Drupal\help\Plugin\Block\HelpBlock

Provides a 'Help' block.

Plugin annotation


@Block(
  id = "help_block",
  admin_label = @Translation("Help"),
  forms = {
    "settings_tray" = FALSE,
  },
)

Hierarchy

Expanded class hierarchy of HelpBlock

File

core/modules/help/src/Plugin/Block/HelpBlock.php, line 24

Namespace

Drupal\help\Plugin\Block
View source
class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface {
    
    /**
     * The module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected $moduleHandler;
    
    /**
     * The current request.
     *
     * @var \Symfony\Component\HttpFoundation\Request
     */
    protected $request;
    
    /**
     * The current route match.
     *
     * @var \Drupal\Core\Routing\RouteMatchInterface
     */
    protected $routeMatch;
    
    /**
     * Creates a HelpBlock instance.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin_id for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The current request.
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler.
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The current route match.
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    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'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function build() {
        // Do not show on a 403 or 404 page.
        if ($this->request->attributes
            ->has('exception')) {
            return [];
        }
        $implementations = $this->moduleHandler
            ->getImplementations('help');
        $build = [];
        $args = [
            $this->routeMatch
                ->getRouteName(),
            $this->routeMatch,
        ];
        foreach ($implementations as $module) {
            // Don't add empty strings to $build array.
            if ($help = $this->moduleHandler
                ->invoke($module, 'help', $args)) {
                // Convert strings to #markup render arrays so that they will XSS admin
                // filtered.
                $build[] = is_array($help) ? $help : [
                    '#markup' => $help,
                ];
            }
        }
        return $build;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return Cache::mergeContexts(parent::getCacheContexts(), [
            'route',
        ]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
HelpBlock::$moduleHandler protected property The module handler.
HelpBlock::$request protected property The current request.
HelpBlock::$routeMatch protected property The current route match.
HelpBlock::build public function
HelpBlock::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
HelpBlock::getCacheContexts public function
HelpBlock::__construct public function Creates a HelpBlock instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.