diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 6f0f863daa6aba5908da13a5542426e1ef66ba87..5683d205b455b68ff58aa2a8a7533b9cd8f2a92a 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -251,10 +251,8 @@ function shortcut_renderable_links($shortcut_set = NULL) { $shortcut_set = shortcut_current_displayed_set(); } - /** @var \Drupal\shortcut\ShortcutInterface[] $shortcuts */ - $shortcuts = \Drupal::entityManager()->getStorage('shortcut')->loadByProperties(array('shortcut_set' => $shortcut_set->id())); $cache_tags = array(); - foreach ($shortcuts as $shortcut) { + foreach ($shortcut_set->getShortcuts() as $shortcut) { $shortcut = \Drupal::entityManager()->getTranslationFromContext($shortcut); $links[$shortcut->id()] = array( 'type' => 'link', diff --git a/core/modules/shortcut/src/Entity/Shortcut.php b/core/modules/shortcut/src/Entity/Shortcut.php index e8e9aedb878f1f329653a4287af48c43893bf231..9fd03325cae9f257cec88ec5c3e2a0e0a6e08bf3 100644 --- a/core/modules/shortcut/src/Entity/Shortcut.php +++ b/core/modules/shortcut/src/Entity/Shortcut.php @@ -181,4 +181,26 @@ public function getCacheTags() { return $this->shortcut_set->entity->getCacheTags(); } + /** + * Sort shortcut objects. + * + * Callback for uasort(). + * + * @param \Drupal\shortcut\ShortcutInterface $a + * First item for comparison. + * @param \Drupal\shortcut\ShortcutInterface $b + * Second item for comparison. + * + * @return int + * The comparison result for uasort(). + */ + public static function sort(ShortcutInterface $a, ShortcutInterface $b) { + $a_weight = $a->getWeight(); + $b_weight = $b->getWeight(); + if ($a_weight == $b_weight) { + return strnatcasecmp($a->getTitle(), $b->getTitle()); + } + return ($a_weight < $b_weight) ? -1 : 1; + } + } diff --git a/core/modules/shortcut/src/Entity/ShortcutSet.php b/core/modules/shortcut/src/Entity/ShortcutSet.php index aa0ccfc8a4c10b7a6ca1fadfab1264fe6bf51fca..692e3abea5cc42835622d3a1b539cabf29c6ee0e 100644 --- a/core/modules/shortcut/src/Entity/ShortcutSet.php +++ b/core/modules/shortcut/src/Entity/ShortcutSet.php @@ -117,7 +117,9 @@ public function resetLinkWeights() { * {@inheritdoc} */ public function getShortcuts() { - return \Drupal::entityManager()->getStorage('shortcut')->loadByProperties(array('shortcut_set' => $this->id())); + $shortcuts = \Drupal::entityManager()->getStorage('shortcut')->loadByProperties(array('shortcut_set' => $this->id())); + uasort($shortcuts, array('\Drupal\shortcut\Entity\Shortcut', 'sort')); + return $shortcuts; } } diff --git a/core/modules/shortcut/src/Form/SetCustomize.php b/core/modules/shortcut/src/Form/SetCustomize.php index e0cf26278a33a3dcb733045ac1ed39f54fa5552c..4c0a268701835f4872df30752d4a978d89ebe2d4 100644 --- a/core/modules/shortcut/src/Form/SetCustomize.php +++ b/core/modules/shortcut/src/Form/SetCustomize.php @@ -79,8 +79,6 @@ public function form(array $form, FormStateInterface $form_state) { '#links' => $links, ); } - // Sort the list so the output is ordered by weight. - uasort($form['shortcuts']['links'], array('\Drupal\Component\Utility\SortArray', 'sortByWeightProperty')); return $form; } diff --git a/core/modules/shortcut/src/ShortcutSetInterface.php b/core/modules/shortcut/src/ShortcutSetInterface.php index 21cc4f350f56041ee4a25e8026ff936693a52201..9d2f941433e4bc3cd95ca769916955a114a2948e 100644 --- a/core/modules/shortcut/src/ShortcutSetInterface.php +++ b/core/modules/shortcut/src/ShortcutSetInterface.php @@ -27,7 +27,7 @@ interface ShortcutSetInterface extends ConfigEntityInterface { public function resetLinkWeights(); /** - * Returns all the shortcuts from a shortcut set. + * Returns all the shortcuts from a shortcut set sorted correctly. * * @return \Drupal\shortcut\ShortcutInterface[] * An array of shortcut entities. diff --git a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php index 40c230d161a6b78727d5ea20d463f914b9c6e9bb..b9c3ee544e60a1b254f3533cf2cb0ec10522cec1 100644 --- a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php @@ -260,6 +260,24 @@ public function testAccessShortcutsPermission() { $this->verifyAccessShortcutsPermissionForEditPages(); } + /** + * Tests the shortcuts are correctly ordered by weight in the toolbar. + */ + public function testShortcutLinkOrder() { + $this->drupalLogin($this->drupalCreateUser(array('access toolbar', 'access shortcuts'))); + $this->drupalGet(Url::fromRoute('<front>')); + $shortcuts = $this->cssSelect('#toolbar-item-shortcuts-tray .menu a'); + $this->assertEqual((string) $shortcuts[0], 'Add content'); + $this->assertEqual((string) $shortcuts[1], 'All content'); + foreach($this->set->getShortcuts() as $shortcut) { + $shortcut->setWeight($shortcut->getWeight() * -1)->save(); + } + $this->drupalGet(Url::fromRoute('<front>')); + $shortcuts = $this->cssSelect('#toolbar-item-shortcuts-tray .menu a'); + $this->assertEqual((string) $shortcuts[0], 'All content'); + $this->assertEqual((string) $shortcuts[1], 'Add content'); + } + /** * Tests that the 'access shortcuts' permission is required for shortcut set * administration page access. diff --git a/core/modules/shortcut/src/Tests/ShortcutSetsTest.php b/core/modules/shortcut/src/Tests/ShortcutSetsTest.php index 34f394dabb971409d5b002ad2ecbe1b0fbd7af90..c321bc473aa2e52ffe0971daa86038e4fc30a772 100644 --- a/core/modules/shortcut/src/Tests/ShortcutSetsTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutSetsTest.php @@ -79,9 +79,10 @@ function testShortcutSetEdit() { $this->drupalPostForm(NULL, $edit, t('Save changes')); $this->assertRaw(t('The shortcut set has been updated.')); - // Check to ensure that the shortcut weights have changed. - $weights = $this->getShortcutInformation($set, 'weight'); - $this->assertEqual($weights, array(2, 1)); + \Drupal::entityManager()->getStorage('shortcut')->resetCache(); + // Check to ensure that the shortcut weights have changed and that + // ShortcutSet::.getShortcuts() returns shortcuts in the new order. + $this->assertIdentical(array_reverse(array_keys($shortcuts)), array_keys($set->getShortcuts())); } /**