Issue #2415645 by geertvd, alexpott: Shortcuts not sorted on display

8.0.x
Nathaniel Catchpole 2015-02-04 12:21:51 +00:00
parent 4691ba9ea4
commit 151e3391c9
7 changed files with 49 additions and 10 deletions

View File

@ -251,10 +251,8 @@ function shortcut_renderable_links($shortcut_set = NULL) {
$shortcut_set = shortcut_current_displayed_set(); $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(); $cache_tags = array();
foreach ($shortcuts as $shortcut) { foreach ($shortcut_set->getShortcuts() as $shortcut) {
$shortcut = \Drupal::entityManager()->getTranslationFromContext($shortcut); $shortcut = \Drupal::entityManager()->getTranslationFromContext($shortcut);
$links[$shortcut->id()] = array( $links[$shortcut->id()] = array(
'type' => 'link', 'type' => 'link',

View File

@ -181,4 +181,26 @@ class Shortcut extends ContentEntityBase implements ShortcutInterface {
return $this->shortcut_set->entity->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;
}
} }

View File

@ -117,7 +117,9 @@ class ShortcutSet extends ConfigEntityBase implements ShortcutSetInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getShortcuts() { 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;
} }
} }

View File

@ -79,8 +79,6 @@ class SetCustomize extends EntityForm {
'#links' => $links, '#links' => $links,
); );
} }
// Sort the list so the output is ordered by weight.
uasort($form['shortcuts']['links'], array('\Drupal\Component\Utility\SortArray', 'sortByWeightProperty'));
return $form; return $form;
} }

View File

@ -27,7 +27,7 @@ interface ShortcutSetInterface extends ConfigEntityInterface {
public function resetLinkWeights(); 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[] * @return \Drupal\shortcut\ShortcutInterface[]
* An array of shortcut entities. * An array of shortcut entities.

View File

@ -260,6 +260,24 @@ class ShortcutLinksTest extends ShortcutTestBase {
$this->verifyAccessShortcutsPermissionForEditPages(); $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 * Tests that the 'access shortcuts' permission is required for shortcut set
* administration page access. * administration page access.

View File

@ -79,9 +79,10 @@ class ShortcutSetsTest extends ShortcutTestBase {
$this->drupalPostForm(NULL, $edit, t('Save changes')); $this->drupalPostForm(NULL, $edit, t('Save changes'));
$this->assertRaw(t('The shortcut set has been updated.')); $this->assertRaw(t('The shortcut set has been updated.'));
// Check to ensure that the shortcut weights have changed. \Drupal::entityManager()->getStorage('shortcut')->resetCache();
$weights = $this->getShortcutInformation($set, 'weight'); // Check to ensure that the shortcut weights have changed and that
$this->assertEqual($weights, array(2, 1)); // ShortcutSet::.getShortcuts() returns shortcuts in the new order.
$this->assertIdentical(array_reverse(array_keys($shortcuts)), array_keys($set->getShortcuts()));
} }
/** /**