Problem/Motivation
The proliferation of UUIDs in both GraphQL and JSON:APIs make the scenario of receiving an array of UUIDs as input. The entity repository can already be used to load a single entity by uuid (EntityRepository::loadEntityByUuid) but using this to load groups of entities is inefficient.
An example use case would be assigning a bunch of tags to a piece of content or adding a lot of users to a group through a mutation. Here the tags or users can be provided as an array of UUIDs.
Steps to reproduce
Proposed resolution
Introduce a EntityRepository::loadEntitiesByUuids method that can load multiple entities of the same type from an array of UUIDs. Behaviour for invalid UUIDs will match the behaviour of EntityStorageInterface::loadMultiple where the array of loaded entities may have a smaller size than the input array of UUIDs and only entities with a valid UUID will be loaded. An empty array of UUIDs will not be supported.
Since adding methods to EntityRepositoryInterface would constitute a breaking change, this change is either limited to 10.0.0 unless a backwards compatible way can be thought of. One method may be adding a separate interface in Drupal 9, but this would complicate the update to Drupal 10 where that interface would stop to exist.
Remaining tasks
- Figure out if it's possible to backport this to 9.x in some way for its utility value
- Implement test coverage
- Implement functionality
- Add change record
User interface changes
None
API changes
Drupal's EntityRepository service now has a loadEntitiesByUuids that can load multiple entities of the same type from an array of UUIDs.
Data model changes
None
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | 3214923-12.patch | 4.98 KB | BS Pavan |
| #12 | interdiff-7-12.txt | 1.21 KB | BS Pavan |
| #11 | 3214923-11.patch | 4.67 KB | dhirendra.mishra |
| #11 | interdiff_5-11.txt | 725 bytes | dhirendra.mishra |
| #9 | 3214923-9.patch | 4.91 KB | BS Pavan |
Comments
Comment #2
kingdutchAttached is a patch that changes
EntityRepositoryInterfacewhich unfortunately makes it backwards incompatible.Comment #3
kingdutchComment #4
rolki commentedChanges look good to me, also I think it is a good idea to add this ability to load multiple entities by UUID. Hope it will be added in the nearest future.
The only thing that should be fixed is "ore", should be "or" in
EntityRepositoryInterface.php line31.Comment #5
ranjith_kumar_k_u commentedFixed typo
Comment #6
jeroentTests fail because of:
Comment #7
imalabyaUpdated the patch to fix the error.
Comment #9
BS Pavan commentedWhen fixing the issue of #7, i noticed that, loadByProperties for field_config cannot take array of values.
Observation:
In FieldConfigStorage.php, line number 141 the foreach statement cannot take multiple array of uuid values. So I think we need to individually pass uuid value to loadByProperties method in EntityRepository class
Please check and let me know if I have analysed correctly.
Thanks
Comment #11
dhirendra.mishra commentedMay be below one would be helpful.
Comment #12
BS Pavan commentedFixing test case failure
Comment #13
berdirthis implementation actually doesn't make it more efficient at all. I see that it was done above due to a problem with config entities. We could try if an entity query works better, but it's already transformed to that internally anyway. Last resort would be to only do a bulk load for content entities, but without doing that, this IMHO is pointless and doesn't provide any improvement compared to just calling loadEntityByUuid() in a loop.
Comment #18
geek-merlinTo fix #13, the for loop should be replaced with sth like: