diff --git a/core/modules/toolbar/config/install/toolbar.settings.yml b/core/modules/toolbar/config/install/toolbar.settings.yml new file mode 100644 index 0000000..7b920b3 --- /dev/null +++ b/core/modules/toolbar/config/install/toolbar.settings.yml @@ -0,0 +1,2 @@ +admin_tray: + menu_name: 'admin' diff --git a/core/modules/toolbar/config/schema/toolbar.schema.yml b/core/modules/toolbar/config/schema/toolbar.schema.yml new file mode 100644 index 0000000..84830ae --- /dev/null +++ b/core/modules/toolbar/config/schema/toolbar.schema.yml @@ -0,0 +1,13 @@ +# Schema for the configuration files of the Toolbar module. + +toolbar.settings: + type: config_object + label: 'Toolbar settings' + mapping: + admin_tray: + type: mapping + label: 'Admin tray settings' + mapping: + menu_name: + type: string + label: 'Menu to use in the toolbar admin tray' diff --git a/core/modules/toolbar/src/Controller/ToolbarController.php b/core/modules/toolbar/src/Controller/ToolbarController.php index c94e35b..382fd7c 100644 --- a/core/modules/toolbar/src/Controller/ToolbarController.php +++ b/core/modules/toolbar/src/Controller/ToolbarController.php @@ -70,16 +70,8 @@ public static function preRenderAdministrationTray(array $element) { // Load the administrative menu. The first level is the "Administration" // link. In order to load the children of that link, start and end on the // second level. - $parameters = new MenuTreeParameters(); - $parameters->setMinDepth(2)->setMaxDepth(2)->onlyEnabledLinks(); - // @todo Make the menu configurable in https://www.drupal.org/node/1869638. - $tree = $menu_tree->load('admin', $parameters); - $manipulators = [ - ['callable' => 'menu.default_tree_manipulators:checkAccess'], - ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], - ['callable' => 'toolbar_menu_navigation_links'], - ]; - $tree = $menu_tree->transform($tree, $manipulators); + /** @var \Drupal\Core\Menu\MenuLinkTreeElement[] $tree */ + $tree = static::getAdminTrayMenuTree(2, 2); $element['administration_menu'] = $menu_tree->build($tree); return $element; } @@ -94,16 +86,8 @@ public static function preRenderGetRenderedSubtrees(array $data) { // Load the administration menu. The first level is the "Administration" // link. In order to load the children of that link and the subsequent two // levels, start at the second level and end at the fourth. - $parameters = new MenuTreeParameters(); - $parameters->setMinDepth(2)->setMaxDepth(4)->onlyEnabledLinks(); - // @todo Make the menu configurable in https://www.drupal.org/node/1869638. - $tree = $menu_tree->load('admin', $parameters); - $manipulators = [ - ['callable' => 'menu.default_tree_manipulators:checkAccess'], - ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], - ['callable' => 'toolbar_menu_navigation_links'], - ]; - $tree = $menu_tree->transform($tree, $manipulators); + /** @var \Drupal\Core\Menu\MenuLinkTreeElement[] $tree */ + $tree = static::getAdminTrayMenuTree(2, 4); $subtrees = []; // Calculated the combined cacheability of all subtrees. $cacheability = new CacheableMetadata(); @@ -140,4 +124,47 @@ public static function trustedCallbacks() { return ['preRenderAdministrationTray', 'preRenderGetRenderedSubtrees']; } + /** + * Returns the name of the menu to use for the 'Manage' tray in the toolbar. + * + * This is here as a separate method to make it easy for contributed and + * custom modules to extend this controller and change the behavior without + * having to duplicate a lot of other code. + * + * @return string + * The machine name of the menu to use for the 'Manage' tray. + * + * @internal + */ + public static function getAdminTrayMenuName() : string { + return \Drupal::config('toolbar.settings')->get('admin_tray.menu_name'); + } + + /** + * Returns the menu tree to use for the 'Manage' tray in the toolbar. + * + * @param int $min_depth + * The minimum depth of menu items to load and use. + * @param int $max_depth + * The maximum depth of menu items to load and use. + * + * @return \Drupal\Core\Menu\MenuLinkTreeElement[] + * The loaded and processed menu tree to use for the 'Manage' tray. + * + * @internal + */ + public static function getAdminTrayMenuTree($min_depth, $max_depth) : array { + $menu_tree = \Drupal::service('toolbar.menu_tree'); + $parameters = new MenuTreeParameters(); + $parameters->setMinDepth($min_depth)->setMaxDepth($max_depth)->onlyEnabledLinks(); + $admin_tray_menu = static::getAdminTrayMenuName(); + $tree = $menu_tree->load($admin_tray_menu, $parameters); + $manipulators = [ + ['callable' => 'menu.default_tree_manipulators:checkAccess'], + ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], + ['callable' => 'toolbar_menu_navigation_links'], + ]; + return $menu_tree->transform($tree, $manipulators); + } + } diff --git a/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/config/install/system.menu.custom.yml b/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/config/install/system.menu.custom.yml new file mode 100644 index 0000000..d0ff9cd --- /dev/null +++ b/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/config/install/system.menu.custom.yml @@ -0,0 +1,6 @@ +langcode: en +status: true +dependencies: { } +id: custom +label: Custom +description: '' diff --git a/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/toolbar_custom_admin_tray_menu.info.yml b/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/toolbar_custom_admin_tray_menu.info.yml new file mode 100644 index 0000000..1664ade --- /dev/null +++ b/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/toolbar_custom_admin_tray_menu.info.yml @@ -0,0 +1,7 @@ +name: 'Custom toolbar admin tray menu' +type: module +description: 'Support module for testing toolbar with a custom admin tray menu.' +package: Testing +version: VERSION +dependencies: + - drupal:toolbar diff --git a/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/toolbar_custom_admin_tray_menu.links.menu.yml b/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/toolbar_custom_admin_tray_menu.links.menu.yml new file mode 100644 index 0000000..f852e69 --- /dev/null +++ b/core/modules/toolbar/tests/modules/toolbar_custom_admin_tray_menu/toolbar_custom_admin_tray_menu.links.menu.yml @@ -0,0 +1,8 @@ +custom.base: + title: Custom menu base link + menu_name: custom + route_name: '' +custom.link: + title: Custom menu link + parent: custom.base + route_name: '' diff --git a/core/modules/toolbar/tests/src/Functional/ToolbarCustomAdminTrayMenuTest.php b/core/modules/toolbar/tests/src/Functional/ToolbarCustomAdminTrayMenuTest.php new file mode 100644 index 0000000..cf19c49 --- /dev/null +++ b/core/modules/toolbar/tests/src/Functional/ToolbarCustomAdminTrayMenuTest.php @@ -0,0 +1,71 @@ +container->get('config.factory')->getEditable('toolbar.settings'); + $config->set('admin_tray.menu_name', 'custom'); + $config->save(); + + // Create an administrative user and log it in. + $this->adminUser = $this->drupalCreateUser([ + 'access toolbar', + 'access administration pages', + ]); + $this->drupalLogin($this->adminUser); + } + + /** + * Tests the custom admin tray menu. + */ + public function testToolbarCustomAdminTrayMenu() { + $this->drupalGet('test-page'); + $this->assertSession()->statusCodeEquals(200); + + // Assert that the toolbar is present in the HTML. + $this->assertRaw('id="toolbar-administration"'); + + // Assert that the "Content" link is not present. + $this->assertNoRaw('href="/admin/content"'); + + // Assert that the custom link is present. + $this->assertRaw('Custom menu link'); + } + +} diff --git a/core/modules/toolbar/tests/src/Functional/ToolbarSettingsUpdateTest.php b/core/modules/toolbar/tests/src/Functional/ToolbarSettingsUpdateTest.php new file mode 100644 index 0000000..fe45eda --- /dev/null +++ b/core/modules/toolbar/tests/src/Functional/ToolbarSettingsUpdateTest.php @@ -0,0 +1,42 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.8.0.bare.standard.php.gz', + ]; + } + + /** + * Ensures that update hook is run for "toolbar" module. + */ + public function testUpdateToolbarSettings() { + $config = $this->config('toolbar.settings'); + $this->assertNull($config->get('admin_tray.menu_name')); + + // Run updates. + $this->runUpdates(); + + $config = $this->config('toolbar.settings'); + $this->assertNotNull($config->get('admin_tray.menu_name')); + } + +} diff --git a/core/modules/toolbar/toolbar.install b/core/modules/toolbar/toolbar.install new file mode 100644 index 0000000..04ace29 --- /dev/null +++ b/core/modules/toolbar/toolbar.install @@ -0,0 +1,16 @@ +getEditable('toolbar.settings') + ->set('admin_tray.menu_name', 'admin') + ->save(); +}