Problem/Motivation
This is a performance enhancement. IncludeResolver::resolveIncludeTree() reads $field_item->entity for each reference item to get the referenced entity's type and ID. If the referenced entities are not already loaded or cached, this causes them to be loaded individually.
The method then calls loadMultiple() with the collected entity IDs. This causes the individually loaded entities to be loaded from the cache.
When the entity property definition specifies the target entity type, we can load in a batch instead of loading individually first and then in a batch.
Steps to reproduce
- Create 5 published articles with 2 distinct tags each.
- Clear the entity caches.
- Enable database query logging.
- Request
/jsonapi/node/article?include=field_tags. - Check the logs. The
taxonomy_term_database table is queried 10 times, once per term.
Proposed resolution
If the entity property definition specifies the target entity type, get the target ID from the item's main property (without loading the entity). The existing loadMultiple() call then loads all targets of that type in one batch.
Skip items with a NULL target ID. Preserve the existing $field_item->entity path for reference fields whose definitions do not specify a target entity type.
User interface changes
None.
API changes
None.
Data model changes
None.
AI disclosure: I used AI to help prepare the IS and MR (Astra and Opus 5.5).
Issue fork drupal-3626240
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
Comment #3
ptmkenny commentedComment #4
ptmkenny commented