We have a thousand-and-one situations in which we need options lists of entities of a particular type, such as for form select elements. We currently have functions or methods per entity type, but let's provide an easily reusable method for developers to do this.

An alternative might be to add a static method to EntityInterface and provide default implementations in the base class(es), so entity types can override their 'list labels'. Currency has quite a few config entities with identical names, because those are currencies that in real life have the same names. There are four different Zimbabwean dollars, for instance. Were someone to build an entity type that references real-world people, they would run into the same problem.

CommentFileSizeAuthor
#1 drupal_2111341_1.patch2.01 KBXano
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Xano’s picture

Assigned: Xano » Unassigned
Status: Active » Needs review
FileSize
2.01 KB
tstoeckler’s picture

  1. +++ b/core/lib/Drupal/Core/Entity/EntityManager.php
    --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityManagerTest.php
    +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityManagerTest.php
    

    Seems we should also test that the options are actually returned correctly for a valid entity type?!

  2. +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityManagerTest.php
    @@ -6,6 +6,7 @@
     namespace Drupal\system\Tests\Entity;
    +use Drupal\views\Plugin\views\display\Page;
    

    Missing newline. More importantly, it seems this class isn't actually used below?

In general I'm not sure adding this is a good idea. For nodes, calling this on a medium to large site, will try to load *all* nodes, so it will easily crash your site.
So I think we should find some way to only add this for config entities. A static method sucks, though, because doesn't have (clean) access to the storage controller or anything.
Maybe this could also use the nifty iterator @dawehner is doing at #2098197: Add getAllRoutes() method to RouteProvider , but maybe that's overkill

Xano’s picture

I should have mentioned that the patch was mostly a proof of concept, especially because @dawehner and I had no idea where to best put a method like this.

Missing newline. More importantly, it seems this class isn't actually used below?

No idea how that got there. I blame PHPStorm.

In general I'm not sure adding this is a good idea. For nodes, calling this on a medium to large site, will try to load *all* nodes, so it will easily crash your site.
So I think we should find some way to only add this for config entities. A static method sucks, though, because doesn't have (clean) access to the storage controller or anything.

Another idea I had was to add this to the list controller, as it essentially returns a list of entity data. We can provide a default implementation in EntityListController, but let specific entity types override the behavior.

tstoeckler’s picture

Another idea I had was to add this to the list controller, as it essentially returns a list of entity data. We can provide a default implementation in EntityListController, but let specific entity types override the behavior.

Yes, I think that makes sense. Can we make it ConfigEntityListController then, please, per the above? I realize that in principal nothing will stop you from having 10,000 content types on the page so even for config entities it *might* be a problem, but it's a much thinner use-case than having 10,000 nodes.

tim.plunkett’s picture

In that case, what problem are we solving? List controllers are swappable, so those calling a function like user_role_names() won't be able to use this directly.

Xano’s picture

@tstoeckler and I discussed the feasibility of instead of letting list controllers just build options, make them build the entire form element used for selection, so content entity list controllers can use autocomplete for performance reasons, and config entities can use select lists. @amateescu overhead us and pointed us to #1959806: Provide a generic 'entity_autocomplete' Form API element, which tries to do a similar thing, but his starting point was Entity Reference, whereas ours is the entity system. If ER becomes required, then we can easily make core use its widgets instead of developing custom solutions per entity type, like we do now.

In that case, what problem are we solving? List controllers are swappable, so those calling a function like user_role_names() won't be able to use this directly.

They are, but adding the method to EntityListControllerInterface ensures that we can always fetch a selector. Swappable list controllers will then be an advantage, because it will allow developers to use a different selector.

Status: Needs review » Needs work

The last submitted patch, drupal_2111341_1.patch, failed testing.

Xano’s picture

Status: Needs work » Needs review

#1: drupal_2111341_1.patch queued for re-testing.

Xano’s picture

#1: drupal_2111341_1.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, drupal_2111341_1.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Berdir’s picture

Issue summary: View changes
Status: Needs work » Closed (won't fix)

Closing this, nobody seemed to be interested in this and I agree that for most entity types, promoting such a use is a bad iea.