From 151e3391c9d92c81df3d3e7960727f44d8d8eb70 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Wed, 4 Feb 2015 12:21:51 +0000 Subject: [PATCH] Issue #2415645 by geertvd, alexpott: Shortcuts not sorted on display --- core/modules/shortcut/shortcut.module | 4 +--- core/modules/shortcut/src/Entity/Shortcut.php | 22 +++++++++++++++++++ .../shortcut/src/Entity/ShortcutSet.php | 4 +++- .../shortcut/src/Form/SetCustomize.php | 2 -- .../shortcut/src/ShortcutSetInterface.php | 2 +- .../shortcut/src/Tests/ShortcutLinksTest.php | 18 +++++++++++++++ .../shortcut/src/Tests/ShortcutSetsTest.php | 7 +++--- 7 files changed, 49 insertions(+), 10 deletions(-) diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 6f0f863daa6a..5683d205b455 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 e8e9aedb878f..9fd03325cae9 100644 --- a/core/modules/shortcut/src/Entity/Shortcut.php +++ b/core/modules/shortcut/src/Entity/Shortcut.php @@ -181,4 +181,26 @@ class Shortcut extends ContentEntityBase implements ShortcutInterface { 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 aa0ccfc8a4c1..692e3abea5cc 100644 --- a/core/modules/shortcut/src/Entity/ShortcutSet.php +++ b/core/modules/shortcut/src/Entity/ShortcutSet.php @@ -117,7 +117,9 @@ class ShortcutSet extends ConfigEntityBase implements ShortcutSetInterface { * {@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 e0cf26278a33..4c0a26870183 100644 --- a/core/modules/shortcut/src/Form/SetCustomize.php +++ b/core/modules/shortcut/src/Form/SetCustomize.php @@ -79,8 +79,6 @@ class SetCustomize extends EntityForm { '#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 21cc4f350f56..9d2f941433e4 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 40c230d161a6..b9c3ee544e60 100644 --- a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php @@ -260,6 +260,24 @@ class ShortcutLinksTest extends ShortcutTestBase { $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('')); + $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('')); + $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 34f394dabb97..c321bc473aa2 100644 --- a/core/modules/shortcut/src/Tests/ShortcutSetsTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutSetsTest.php @@ -79,9 +79,10 @@ class ShortcutSetsTest extends ShortcutTestBase { $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())); } /**