diff --git a/core/modules/views/src/Entity/View.php b/core/modules/views/src/Entity/View.php index 19d13c1..683981d 100644 --- a/core/modules/views/src/Entity/View.php +++ b/core/modules/views/src/Entity/View.php @@ -301,6 +301,11 @@ public function calculateDependencies() { public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); + // Sort the displays. + $display = $this->get('display'); + ksort($display); + $this->set('display', array('default' => $display['default']) + $display); + // @todo Check whether isSyncing is needed. if (!$this->isSyncing()) { $this->addCacheMetadata(); @@ -437,13 +442,6 @@ public function mergeDefaultDisplaysOptions() { // Add the defaults for the display. $displays[$key] = $options; } - // Sort the displays. - uasort($displays, function ($display1, $display2) { - if ($display1['position'] != $display2['position']) { - return $display1['position'] < $display2['position'] ? -1 : 1; - } - return 0; - }); $this->set('display', $displays); } diff --git a/core/modules/views/src/Tests/ViewStorageTest.php b/core/modules/views/src/Tests/ViewStorageTest.php index 7a05428..07569a3 100644 --- a/core/modules/views/src/Tests/ViewStorageTest.php +++ b/core/modules/views/src/Tests/ViewStorageTest.php @@ -97,7 +97,7 @@ protected function loadTests() { } // Check the displays have been loaded correctly from config display data. - $expected_displays = array('default', 'page_1', 'block_1'); + $expected_displays = array('default', 'block_1', 'page_1'); $this->assertEqual(array_keys($view->get('display')), $expected_displays, 'The correct display names are present.'); // Check each ViewDisplay object and confirm that it has the correct key and diff --git a/core/modules/views_ui/src/Tests/DisplayOrderTest.php b/core/modules/views_ui/src/Tests/DisplayOrderTest.php new file mode 100644 index 0000000..54ccd09 --- /dev/null +++ b/core/modules/views_ui/src/Tests/DisplayOrderTest.php @@ -0,0 +1,69 @@ +set('ui.show.master_display', TRUE) + ->save(); + + $settings['page[create]'] = FALSE; + $view = $this->randomView($settings); + + $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit'; + $this->drupalGet($path_prefix); + + // Add a new page display. + $this->drupalPostForm(NULL, array(), 'Add Page'); + $this->assertLinkByHref($path_prefix . '/page_1', 0, 'Make sure after adding a display the new display appears in the UI'); + + $this->assertNoLink('Master*', 0, 'Make sure the master display is not marked as changed.'); + $this->assertLink('Page*', 0, 'Make sure the added display is marked as changed.'); + + $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_1/path", array('path' => 'test/path'), t('Apply')); + $this->drupalPostForm(NULL, array(), t('Save')); + + // Add a new block display. + $this->drupalPostForm(NULL, array(), 'Add Block'); + $this->assertLinkByHref($path_prefix . '/block_1', 0, 'Make sure after adding a display the new display appears in the UI'); + + // Test that the new view displays are in the correct order. + $view = Views::getView($view['id']); + $displays = $view->storage->get('display'); + $expected_displays = array('default', 'block_1', 'page_1'); + $this->assertEqual(array_keys($displays), $expected_displays, 'The display names are in correct order.'); + } + +} diff --git a/core/modules/views_ui/src/ViewFormBase.php b/core/modules/views_ui/src/ViewFormBase.php index 7747078..62897f7 100644 --- a/core/modules/views_ui/src/ViewFormBase.php +++ b/core/modules/views_ui/src/ViewFormBase.php @@ -54,6 +54,21 @@ protected function prepareEntity() { if ($tabs = $this->getDisplayTabs($this->entity)) { // If a display isn't specified, use the first one. if (empty($this->displayID)) { + + uksort($tabs, function($a, $b) { + // Prioritize: default, page, block, other ones. + if (strpos($a, 'default') === 0 || strpos($b, 'default') === 0) { + return -1; + } + if (strpos($a, 'page') === 0 || strpos($b, 'page') === 0) { + return -1; + } + if (strpos($a, 'block') === 0 || strpos($b, 'block') === 0) { + return -1; + } + return $a < $b ? -1 : 1; + }); + foreach ($tabs as $id => $tab) { if (!isset($tab['#access']) || $tab['#access']) { $this->displayID = $id;