You are here

class HelpBlock in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/help/src/Plugin/Block/HelpBlock.php \Drupal\help\Plugin\Block\HelpBlock
  2. 9 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 [];
    }
    $build = [];
    $this->moduleHandler
      ->invokeAllWith('help', function (callable $hook, string $module) use (&$build) {

      // Don't add empty strings to $build array.
      if ($help = $hook($this->routeMatch
        ->getRouteName(), $this->routeMatch)) {

        // 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