diff --git a/core/lib/Drupal/Core/Config/Entity/Query/Query.php b/core/lib/Drupal/Core/Config/Entity/Query/Query.php index b8660aa..ea3b7a9 100644 --- a/core/lib/Drupal/Core/Config/Entity/Query/Query.php +++ b/core/lib/Drupal/Core/Config/Entity/Query/Query.php @@ -74,10 +74,10 @@ public function execute() { // Apply sort settings. foreach ($this->sort as $sort) { - $direction = $sort['direction'] == 'ASC' ? -1 : 1; + $direction = $sort['direction'] == 'ASC' ? 1 : -1; $field = $sort['field']; uasort($result, function($a, $b) use ($field, $direction) { - return ($a[$field] <= $b[$field]) ? $direction : -$direction; + return strnatcmp($a[$field], $b[$field]) * $direction; }); } diff --git a/core/lib/Drupal/Core/Entity/EntityListBuilder.php b/core/lib/Drupal/Core/Entity/EntityListBuilder.php index 7137951..6723fdc 100644 --- a/core/lib/Drupal/Core/Entity/EntityListBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php @@ -88,11 +88,20 @@ public function load() { /** * Loads entity IDs using a pager. * + * Results sorted by the entity label if it exists, otherwise by entity id. + * * @return array * An array of entity IDs. */ protected function getEntityIds() { $query = $this->getStorage()->getQuery(); + $keys = $this->entityType->getKeys(); + if (isset($keys['label']) && $this->entityType->getClass() instanceof ) { + $query->sort($keys['label']); + } + else { + $query->sort($keys['id']); + } return $query ->pager($this->limit) ->execute(); diff --git a/core/modules/config/src/Tests/ConfigEntityListTest.php b/core/modules/config/src/Tests/ConfigEntityListTest.php index 275136d..6964061 100644 --- a/core/modules/config/src/Tests/ConfigEntityListTest.php +++ b/core/modules/config/src/Tests/ConfigEntityListTest.php @@ -247,7 +247,7 @@ public function testPager() { for ($i = 1; $i < 52; $i++) { $storage->create(array( 'id' => str_pad($i, 2, '0', STR_PAD_LEFT), - 'label' => 'Test config entity ' . $i, + 'label' => 'Config entity ' . $i, 'weight' => $i, 'protected_property' => $i, ))->save(); @@ -257,14 +257,14 @@ public function testPager() { $this->drupalGet('admin/structure/config_test'); // Item 51 should not be present. - $this->assertRaw('Test config entity 50', 'Config entity 50 is shown.'); - $this->assertNoRaw('Test config entity 51', 'Config entity 51 is on the next page.'); + $this->assertRaw('Config entity 50', 'Config entity 50 is shown.'); + $this->assertNoRaw('Config entity 51', 'Config entity 51 is on the next page.'); // Browse to the next page. $this->clickLink(t('Page 2')); - $this->assertNoRaw('Test config entity 50', 'Test config entity 50 is on the previous page.'); + $this->assertNoRaw('Config entity 50', 'Config entity 50 is on the previous page.'); $this->assertRaw('dotted.default', 'Default config entity appears on page 2.'); - $this->assertRaw('Test config entity 51', 'Test config entity 51 is on page 2.'); + $this->assertRaw('Config entity 51', 'Config entity 51 is on page 2.'); } }