Add method to EntityRepository to load multiple entities by UUID

Created on 20 May 2021, over 3 years ago
Updated 20 September 2024, 3 months ago

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

Feature request
Status

Needs work

Version

11.0 🔥

Component
Entity 

Last updated about 17 hours ago

Created by

🇳🇱Netherlands kingdutch

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪Germany geek-merlin Freiburg, Germany

    To fix #13, the for loop should be replaced with sth like:

    $entities = $this->entityTypeManager->getStorage($entity_type_id)->loadByProperties([$uuid_key => $uuids]);
    
Production build 0.71.5 2024