Problem/Motivation

This was descoped from #2693485: Content types are ordered by machine name on /node/add page (+ similar issues with other entities)

Steps to reproduce

Proposed resolution

Sort bundles suing config entity sort if they are config entities - without loading the config entities too many times for descriptions and sorting.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3196798

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alexpott created an issue. See original summary.

paulocs’s picture

Assigned: Unassigned » paulocs
paulocs’s picture

Assigned: paulocs » Unassigned
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new3.25 KB

I added a patch to sort the bundles. The patch contains the fix and the test cases.
I also changed the issue summary as it was referencing to the wrong issue.

juhog’s picture

Assigned: Unassigned » juhog
Status: Needs review » Active

Regarding #3:

+++ b/core/lib/Drupal/Core/Entity/Controller/EntityController.php
@@ -336,6 +336,8 @@ protected function loadBundleDescriptions(array $bundles, EntityTypeInterface $b
+    $ordered_labels = array_column($bundles, 'label');
+    array_multisort($ordered_labels, $bundles);

This custom sort logic probably won't be suitable. I believe the sorting needs to be done using sort() method of the bundle entity type.

One idea suggested in the parent issue (here) was to use loadBundleDescriptions() also for sorting the bundles. I'll give that a go, patch coming up shortly.

juhog’s picture

Status: Active » Needs review
StatusFileSize
new4.24 KB
new2 KB
new28.16 KB

This patch implements the idea brought up by alexpott in the parent ticket comment #132.

Not doing this as part of loadBundleDescriptions means we have to load the bundles twice which feels wasteful. It's tempting to make loadBundleDescriptions return the sorted list? If it did then contrib/custom usages would be fixed - see http://grep.xnddx.ru/search?text=loadBundleDescriptions&filename= But that has the downside of then becoming a really bad method name.

One concern in my implementation: I'm assuming bundle entity types always extend ConfigEntityBase. So I didn't add an if-clause to check whether sort() is callable or not. But I'm not sure if that's the case.

---

I'd like to ask about the bundle information array though:

Is it mandatory to always use EntityTypeBundleInfo::getBundleInfo() to get the list of bundles?

Seems like addPage() code could be simpler if bundle information array was used only when there is no bundle entity type. That would probably break some backwards compatibility though. Before returning the results, the bundle information service allows modules to add/remove some bundles by triggering alter('entity_bundle_info'). So I suppose addPage() must keep using getBundleInfo() in all situations?

juhog’s picture

Assigned: juhog » Unassigned

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new143 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

ranjith_kumar_k_u’s picture

StatusFileSize
new685 bytes
new4.37 KB

#5 failed to apply on 10.1x

error: patch failed: core/modules/system/tests/src/Functional/Entity/EntityAddUITest.php:72
error: core/modules/system/tests/src/Functional/Entity/EntityAddUITest.php: patch does not apply

ranjith_kumar_k_u’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Tried testing this without the patch by going to node/add
I updated the labels multiple times to change the order and each time the order updates correctly.

Not entirely clear what this could be fixing? Could someone elaborate please.

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Nothing the change actually on admin/structure/types screen.

catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Entity/Controller/EntityController.php
@@ -340,22 +340,29 @@ protected function doGetEntity(RouteMatchInterface $route_match, EntityInterface
    */
   protected function loadBundleDescriptions(array $bundles, EntityTypeInterface $bundle_entity_type) {
-    if (!$bundle_entity_type->entityClassImplements(EntityDescriptionInterface::class)) {
-      return $bundles;
-    }
     $bundle_names = array_keys($bundles);

I don't think the early return should have been changed here. If for some reason we need to sort in this case to, it might need to happen separately.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley made their first commit to this issue’s fork.

acbramley’s picture

Title: Fix \Drupal\Core\Entity\Controller\EntityController so bundles are sorted by label and not ID » Fix EntityController::addPage so bundles are sorted by label and not ID
Status: Needs work » Needs review
Related issues: +#3346394: Replace BlockContentController::add with EntityController::addPage via the AdminHtmlRouteProvider route provider

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Ran the test-only job here https://git.drupalcode.org/issue/drupal-3196798/-/jobs/5609439 which gave

1) Drupal\Tests\system\Functional\Entity\EntityAddUITest::testAddPageWithBundleEntities
Behat\Mink\Exception\ResponseTextException: The pattern /Aaa Test2 label(.*)Test label/ was not found anywhere in the text of the current page.
/builds/issue/drupal-3196798/vendor/behat/mink/src/WebAssert.php:907
/builds/issue/drupal-3196798/vendor/behat/mink/src/WebAssert.php:329
/builds/issue/drupal-3196798/core/modules/system/tests/src/Functional/Entity/EntityAddUITest.php:79
FAILURES!

Awesome use of extending an existing test!

Fix to core/lib/Drupal/Core/Entity/Controller/EntityController.php seems pretty straight forward with the uasort. No objections

LGTM

  • larowlan committed 3d929061 on 11.2.x
    Issue #3196798 by acbramley, juhog, ranjith_kumar_k_u, paulocs,...

  • larowlan committed 025184ce on 11.x
    Issue #3196798 by acbramley, juhog, ranjith_kumar_k_u, paulocs,...
larowlan’s picture

Version: 11.x-dev » 11.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed to 11.x and backported to 11.2.x as there is minimal risk of disruption here.

Thanks folks

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.