LocalActionsBlock.php 2.3 KB

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