Commit 151e3391 authored by catch's avatar catch
Browse files

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

parent 4691ba9e
......@@ -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',
......
......@@ -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;
}
}
......@@ -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;
}
}
......@@ -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;
}
......
......@@ -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.
......
......@@ -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.
......
......@@ -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()));
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment