Getting the value of target_uuid loads referenced entity

Created on 7 April 2020, about 5 years ago
Updated 3 April 2025, about 1 month ago

Problem/Motivation

This logic in EntityReferenceUuidItem::onChange seems to be triggered even when just getting the value on an entity (via $entity->field_name->target_uuid)

    elseif ($property_name == 'target_uuid') {
      $property = $this->get('entity');
      $entity_type = $property->getDataDefinition()->getConstraint('EntityType');
      $entities =  \Drupal::entityTypeManager()->getStorage($entity_type)->loadByProperties(['uuid' => $this->get('target_uuid')->getValue()]);
      if ($entity =  array_shift($entities)) {
        $this->writePropertyValue('target_id', $entity->id());
        $this->writePropertyValue('entity', $entity);
      }

Proposed resolution

Only load the entity if needed (and then, perhaps utilize an internal cache of entities already loaded?)

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jhedstrom Portland, OR

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.

  • heddn Nicaragua

    This is the closest open issue. Drive-by review on the current code that is being changed here. PHPStan currently complains about:

    public function onChange()
    <snip>
        $entity_type = $property->getDataDefinition()->getConstraint('EntityType')[''];
        $entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadByProperties(['uuid' => $this->get('target_uuid')->getValue()]);
    <snip>
    

    $entity_type that is returned is an error and getStorage requires a string.

Production build 0.71.5 2024