diff --git a/core/modules/toolbar/js/toolbar.es6.js b/core/modules/toolbar/js/toolbar.es6.js index f715fc7fc9..cf795b5764 100644 --- a/core/modules/toolbar/js/toolbar.es6.js +++ b/core/modules/toolbar/js/toolbar.es6.js @@ -141,9 +141,13 @@ // defined then show the tray of the first toolbar tab by default (but // not the first 'Home' toolbar tab). if (Drupal.toolbar.models.toolbarModel.get('orientation') === 'horizontal' && Drupal.toolbar.models.toolbarModel.get('activeTab') === null) { - Drupal.toolbar.models.toolbarModel.set({ - activeTab: $('.toolbar-bar .toolbar-tab:not(.home-toolbar-tab) a').get(0), - }); + const firstItem = $('.toolbar-bar .toolbar-tab:not(.home-toolbar-tab) a'); + const attr = firstItem.attr('data-toolbar-tray'); + if (attr !== undefined) { + Drupal.toolbar.models.toolbarModel.set({ + activeTab: firstItem.get(0), + }); + } } }); }, diff --git a/core/modules/toolbar/js/toolbar.js b/core/modules/toolbar/js/toolbar.js index 547bcd9bac..424d87ccbe 100644 --- a/core/modules/toolbar/js/toolbar.js +++ b/core/modules/toolbar/js/toolbar.js @@ -95,9 +95,13 @@ }); if (Drupal.toolbar.models.toolbarModel.get('orientation') === 'horizontal' && Drupal.toolbar.models.toolbarModel.get('activeTab') === null) { - Drupal.toolbar.models.toolbarModel.set({ - activeTab: $('.toolbar-bar .toolbar-tab:not(.home-toolbar-tab) a').get(0) - }); + var firstItem = $('.toolbar-bar .toolbar-tab:not(.home-toolbar-tab) a'); + var attr = firstItem.attr('data-toolbar-tray'); + if (attr !== undefined) { + Drupal.toolbar.models.toolbarModel.set({ + activeTab: firstItem.get(0) + }); + } } }); } diff --git a/core/modules/toolbar/tests/modules/toolbar_test/toolbar_test.module b/core/modules/toolbar/tests/modules/toolbar_test/toolbar_test.module index 323af166c6..37dcef44bb 100644 --- a/core/modules/toolbar/tests/modules/toolbar_test/toolbar_test.module +++ b/core/modules/toolbar/tests/modules/toolbar_test/toolbar_test.module @@ -45,5 +45,23 @@ function toolbar_test_toolbar() { '#weight' => 50, ]; + if (\Drupal::state()->get('toolbar_test__testing_item_enabled', FALSE)) { + $items['testing_item'] = [ + '#type' => 'toolbar_item', + 'tab' => [ + '#type' => 'link', + '#title' => t('Test item'), + '#url' => Url::fromRoute(''), + '#options' => [ + 'attributes' => [ + 'id' => 'toolbar-item-testing', + 'title' => t('Test item'), + ], + ], + ], + '#weight' => -50, + ]; + } + return $items; } diff --git a/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php b/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php index c3b3d9abc6..5efff4a745 100644 --- a/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php +++ b/core/modules/toolbar/tests/src/FunctionalJavascript/ToolbarIntegrationTest.php @@ -14,7 +14,7 @@ class ToolbarIntegrationTest extends JavascriptTestBase { /** * {@inheritdoc} */ - public static $modules = ['toolbar', 'node']; + public static $modules = ['toolbar', 'node', 'toolbar_test']; /** * Tests if the toolbar can be toggled with JavaScript. @@ -30,6 +30,11 @@ public function testToolbarToggling() { $this->drupalGet(''); $page = $this->getSession()->getPage(); + $session = $this->getSession(); + $result = $session->evaluateScript("Drupal.toolbar.models.toolbarModel.get('activeTab').id"); + // Ensure that the administration toolbar tab is active by default. + $this->assertEquals('toolbar-item-administration', $result); + // Test that it is possible to toggle the toolbar tray. $content = $page->findLink('Content'); $this->assertTrue($content->isVisible(), 'Toolbar tray is open by default.'); @@ -40,6 +45,7 @@ public function testToolbarToggling() { // Test toggling the toolbar tray between horizontal and vertical. $tray = $page->findById('toolbar-item-administration-tray'); + $this->createScreenshot('/tmp/foo.png'); $this->assertFalse($tray->hasClass('toolbar-tray-vertical'), 'Toolbar tray is not vertically oriented by default.'); $page->pressButton('Vertical orientation'); $this->assertTrue($tray->hasClass('toolbar-tray-vertical'), 'After toggling the orientation the toolbar tray is now displayed vertically.'); @@ -48,4 +54,25 @@ public function testToolbarToggling() { $this->assertTrue($tray->hasClass('toolbar-tray-horizontal'), 'After toggling the orientation a second time the toolbar tray is displayed horizontally again.'); } + /** + * Tests a toolbar with the first item not being a tab. + */ + public function testToolbarItem() { + \Drupal::state()->set('toolbar_test__testing_item_enabled', TRUE); + $admin_user = $this->drupalCreateUser([ + 'access toolbar', + 'administer site configuration', + 'access content overview', + ]); + $this->drupalLogin($admin_user); + + $this->drupalGet(''); + $this->getSession()->getPage(); + + $session = $this->getSession(); + $result = $session->evaluateScript("Drupal.toolbar.models.toolbarModel.get('activeTab');"); + // Ensure that there is no active tab by default. + $this->assertNull($result); + } + }