diff --git a/core/modules/block/src/BlockListBuilder.php b/core/modules/block/src/BlockListBuilder.php index 293f088..f556372 100644 --- a/core/modules/block/src/BlockListBuilder.php +++ b/core/modules/block/src/BlockListBuilder.php @@ -15,6 +15,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Operations\OperationsProviderInterface; use Drupal\Core\Form\FormInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; @@ -382,6 +383,10 @@ public function getDefaultOperations(EntityInterface $entity) { $operations['edit']['title'] = t('Configure'); } + if ($entity instanceof OperationsProviderInterface) { + $operations += $entity->getOperationLinks(); + } + return $operations; } diff --git a/core/modules/block/src/Entity/Block.php b/core/modules/block/src/Entity/Block.php old mode 100644 new mode 100755 index 4af2f80..7186a31 --- a/core/modules/block/src/Entity/Block.php +++ b/core/modules/block/src/Entity/Block.php @@ -15,6 +15,7 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Entity\EntityWithPluginCollectionInterface; use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Operations\OperationsProviderInterface; /** * Defines a Block configuration entity class. @@ -41,7 +42,7 @@ * } * ) */ -class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginCollectionInterface { +class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginCollectionInterface, OperationsProviderInterface { /** * The ID of the block. @@ -335,4 +336,16 @@ public function createDuplicateBlock($new_id = NULL, $new_theme = NULL) { return $duplicate; } + /** + * {@inheritdoc} + */ + public function getOperationLinks() { + $plugin = $this->getPlugin(); + $links = []; + if ($plugin instanceof OperationsProviderInterface) { + $links = $plugin->getOperationLinks(); + } + return $links; + } + } diff --git a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php index 819ff55..8e123cb 100644 --- a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php @@ -9,10 +9,13 @@ use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Menu\MenuActiveTrailInterface; use Drupal\Core\Menu\MenuLinkTreeInterface; +use Drupal\Core\Operations\OperationsProviderInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -25,7 +28,7 @@ * deriver = "Drupal\system\Plugin\Derivative\SystemMenuBlock" * ) */ -class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterface { +class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterface, OperationsProviderInterface { /** * The menu link tree service. @@ -42,6 +45,11 @@ class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterfa protected $menuActiveTrail; /** + * @var \Drupal\Core\Extension\ModuleHandlerInterface. + */ + protected $moduleHandler; + + /** * Constructs a new SystemMenuBlock. * * @param array $configuration @@ -54,11 +62,14 @@ class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterfa * The menu tree service. * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail * The active menu trail service. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail, ModuleHandlerInterface $module_handler) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->menuTree = $menu_tree; $this->menuActiveTrail = $menu_active_trail; + $this->moduleHandler = $module_handler; } /** @@ -70,7 +81,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->get('menu.link_tree'), - $container->get('menu.active_trail') + $container->get('menu.active_trail'), + $container->get('module_handler') ); } @@ -211,4 +223,21 @@ protected function getRequiredCacheContexts() { return array('cache_context.user.roles', 'cache_context.language'); } + /** + * {@inheritdoc} + */ + public function getOperationLinks() { + $menu = $this->getDerivativeId(); + + $links = []; + if ($this->moduleHandler->moduleExists('menu_ui')) { + $links['menu-edit'] = [ + 'title' => $this->t('Edit menu'), + 'url' => Url::fromRoute('entity.menu.edit_form', ['menu' => $menu]), + 'weight' => 50, + ]; + } + return $links; + } + } diff --git a/core/modules/system/src/Tests/Block/SystemMenuBlockTest.php b/core/modules/system/src/Tests/Block/SystemMenuBlockTest.php index 0361eb7..2db3ddb 100644 --- a/core/modules/system/src/Tests/Block/SystemMenuBlockTest.php +++ b/core/modules/system/src/Tests/Block/SystemMenuBlockTest.php @@ -6,6 +6,7 @@ namespace Drupal\system\Tests\Block; use Drupal\Core\Render\Element; +use Drupal\Core\Url; use Drupal\simpletest\KernelTestBase; use Drupal\system\Tests\Routing\MockRouteProvider; use Drupal\Tests\Core\Menu\MenuLinkMock; @@ -14,6 +15,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Drupal\user\Entity\Role; /** * Tests \Drupal\system\Plugin\Block\SystemMenuBlock. @@ -37,6 +39,7 @@ class SystemMenuBlockTest extends KernelTestBase { 'block', 'menu_test', 'menu_link_content', + 'menu_ui', 'field', 'user', 'link', @@ -87,6 +90,19 @@ protected function setUp() { $this->installSchema('system', array('router')); $this->installEntitySchema('menu_link_content'); + $user = User::create([ + 'name' => 'tony', + 'mail' => 'tony@example.com', + ]); + $role = Role::create([ + 'label' => 'Menu manager', + 'id' => 'menu_manager', + ]); + $role->grantPermission('administer menu'); + $role->save(); + $user->addRole($role->id()); + \Drupal::service('current_user')->setAccount($user); + $account = User::create([ 'name' => $this->randomMachineName(), 'status' => 1, @@ -177,6 +193,27 @@ public function testSystemMenuBlockConfigDependencies() { } /** + * Tests the editing links for SystemMenuBlock. + */ + public function testOperationLinks() { + + $block = entity_create('block', array( + 'plugin' => 'system_menu_block:' . $this->menu->id(), + 'region' => 'footer', + 'id' => 'machinename', + 'theme' => 'stark', + )); + + $links = $block->getOperationLinks(); + $menu_link = [ + 'title' => 'Edit menu', + 'url' => Url::fromRoute('entity.menu.edit_form', ['menu' => $this->menu->id()]), + 'weight' => 50, + ]; + $this->assertEqual($links, ['menu-edit' => $menu_link]); + } + + /** * Tests the config start level and depth. */ public function testConfigLevelDepth() {