Verified Commit 3d929061 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3196798 by acbramley, juhog, ranjith_kumar_k_u, paulocs, smustgrave,...

Issue #3196798 by acbramley, juhog, ranjith_kumar_k_u, paulocs, smustgrave, alexpott, catch: Fix EntityController::addPage so bundles are sorted by label and not ID

(cherry picked from commit 025184ce)
parent 7d9bdc0a
Loading
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -335,13 +335,16 @@ protected function doGetEntity(RouteMatchInterface $route_match, ?EntityInterfac
  /**
   * Expands the bundle information with descriptions, if known.
   *
   * Also sorts the bundles before adding the bundle descriptions. Sorting is
   * being done here to avoid having to load bundle entities multiple times.
   *
   * @param array $bundles
   *   An array of bundle information.
   * @param \Drupal\Core\Entity\EntityTypeInterface $bundle_entity_type
   *   The bundle entity type definition.
   *
   * @return array
   *   The expanded array of bundle information.
   *   An array of sorted bundle information including bundle descriptions.
   */
  protected function loadBundleDescriptions(array $bundles, EntityTypeInterface $bundle_entity_type) {
    if (!$bundle_entity_type->entityClassImplements(EntityDescriptionInterface::class)) {
@@ -351,6 +354,10 @@ protected function loadBundleDescriptions(array $bundles, EntityTypeInterface $b
    $storage = $this->entityTypeManager->getStorage($bundle_entity_type->id());
    /** @var \Drupal\Core\Entity\EntityDescriptionInterface[] $bundle_entities */
    $bundle_entities = $storage->loadMultiple($bundle_names);

    uasort($bundle_entities, [$bundle_entity_type->getClass(), 'sort']);
    $bundles = array_replace($bundle_entities, $bundles);

    foreach ($bundles as $bundle_name => &$bundle_info) {
      if (isset($bundle_entities[$bundle_name])) {
        $bundle_info['description'] = $bundle_entities[$bundle_name]->getDescription();
+9 −6
Original line number Diff line number Diff line
@@ -62,20 +62,23 @@ public function testAddPageWithBundleEntities(): void {
    $this->drupalGet('/entity_test_with_bundle/add');
    $this->assertSession()->addressEquals('/entity_test_with_bundle/add/test');

    // Two bundles exist, confirm both are shown.
    // Two bundles exist. Confirm both are shown and that they are ordered
    // alphabetically by their labels, not by their IDs.
    EntityTestBundle::create([
      'id' => 'test2',
      'label' => 'Test2 label',
      'label' => 'Aaa Test2 label',
      'description' => 'My test2 description',
    ])->save();
    $this->drupalGet('/entity_test_with_bundle/add');

    $this->assertSession()->linkExists('Test label');
    $this->assertSession()->linkExists('Test2 label');
    $this->assertSession()->linkExists('Aaa Test2 label');
    $this->assertSession()->pageTextContains('My test description');
    $this->assertSession()->pageTextContains('My test2 description');

    $this->clickLink('Test2 label');
    $this->assertSession()->pageTextMatches('/Aaa Test2 label(.*)Test label/');

    $this->clickLink('Aaa Test2 label');
    $this->drupalGet('/entity_test_with_bundle/add/test2');

    $this->submitForm(['name[0][value]' => 'test name'], 'Save');
@@ -106,7 +109,7 @@ public function testAddPageWithBundleEntities(): void {
    $this->drupalGet('/entity_test_with_bundle/add');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->linkExists('Test label');
    $this->assertSession()->linkExists('Test2 label');
    $this->assertSession()->linkExists('Aaa Test2 label');
    $this->assertSession()->linkNotExists('Forbidden to create bundle');
    $this->assertSession()->linkNotExists('Test3 label');
    $this->clickLink('Test label');
@@ -129,7 +132,7 @@ public function testAddPageWithBundleEntities(): void {
    $this->drupalGet('/entity_test_with_bundle/add');
    $this->assertSession()->linkNotExists('Forbidden to create bundle');
    $this->assertSession()->linkNotExists('Test label');
    $this->assertSession()->linkNotExists('Test2 label');
    $this->assertSession()->linkNotExists('Aaa Test2 label');
    $this->assertSession()->linkNotExists('Test3 label');
    $this->assertSession()->linkExists('Add a new test entity bundle.');
  }