1<?php
2
3namespace Drupal\Core\Menu\Plugin\Block;
4
5use Drupal\Core\Block\BlockBase;
6use Drupal\Core\Menu\LocalActionManagerInterface;
7use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8use Symfony\Component\DependencyInjection\ContainerInterface;
9use Drupal\Core\Routing\RouteMatchInterface;
10
11/**
12 * Provides a block to display the local actions.
13 *
14 * @Block(
15 *   id = "local_actions_block",
16 *   admin_label = @Translation("Primary admin actions")
17 * )
18 */
19class LocalActionsBlock extends BlockBase implements ContainerFactoryPluginInterface {
20
21  /**
22   * The local action manager.
23   *
24   * @var \Drupal\Core\Menu\LocalActionManagerInterface
25   */
26  protected $localActionManager;
27
28  /**
29   * The route match.
30   *
31   * @var \Drupal\Core\Routing\RouteMatchInterface
32   */
33  protected $routeMatch;
34
35  /**
36   * Creates a LocalActionsBlock instance.
37   *
38   * @param array $configuration
39   *   A configuration array containing information about the plugin instance.
40   * @param string $plugin_id
41   *   The plugin_id for the plugin instance.
42   * @param mixed $plugin_definition
43   *   The plugin implementation definition.
44   * @param \Drupal\Core\Menu\LocalActionManagerInterface $local_action_manager
45   *   A local action manager.
46   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
47   *   The route match.
48   */
49  public function __construct(array $configuration, $plugin_id, $plugin_definition, LocalActionManagerInterface $local_action_manager, RouteMatchInterface $route_match) {
50    parent::__construct($configuration, $plugin_id, $plugin_definition);
51    $this->localActionManager = $local_action_manager;
52    $this->routeMatch = $route_match;
53  }
54
55  /**
56   * {@inheritdoc}
57   */
58  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
59    return new static(
60      $configuration,
61      $plugin_id,
62      $plugin_definition,
63      $container->get('plugin.manager.menu.local_action'),
64      $container->get('current_route_match')
65    );
66  }
67
68  /**
69   * {@inheritdoc}
70   */
71  public function defaultConfiguration() {
72    return ['label_display' => FALSE];
73  }
74
75  /**
76   * {@inheritdoc}
77   */
78  public function build() {
79    $route_name = $this->routeMatch->getRouteName();
80    $local_actions = $this->localActionManager->getActionsForRoute($route_name);
81
82    return $local_actions;
83  }
84
85}
86