Logic error inside EntityReferenceFormatterBase::needsEntityLoad

Created on 2 January 2018, over 7 years ago
Updated 13 February 2025, about 2 months ago

Problem

EntityReferenceFormatterBase::needsEntityLoad contains a logic error.

Current implementation:

<?php
protected function needsEntityLoad(EntityReferenceItem $item) {
  return !$item->hasNewEntity();
}
?>

Above implementation always returns TRUE in case the item doesn't have a new entity. This implies to return also TRUE in the following cases, but shouldn't:

  1. The field item already loaded its entity, i.e. no need for loading it again
  2. The field item is empty, i.e. doesn't have any reference to a (non-new) entity

While the first case is a performance consideration, the second case might cause faulty situations e.g. when extending EntityReferenceFormatterBase.

A plugin extending EntityReferenceFormatterBase which appends further entities, e.g. like this (pseudocode):

<?php
class ContentTeaserFormatter extends EntityReferenceEntityFormatter {
  public function prepareView(array $entities_items) {
      $content = Node::load(123);
      foreach ($entities_items as $items) {
        $items->set($delta, $content);
        parent::prepareView($entities_items);
      }
    }
  }
}
?>

This would throw following error:

Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 227 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).
Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) (Line: 139)
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->prepareView(Array) (Line: 189)

Proposed solution

Replace current implementation with

<?php
    if (isset($item->entity) && $item->entity instanceof EntityInterface) {
      $item->_loaded = TRUE;
    }
    else {
      $item->_loaded = FALSE;
    }
    return isset($item->target_id) && !$item->_loaded;
?>

See also attached patch.

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

field system

Created by

πŸ‡©πŸ‡ͺGermany mxh Offenburg

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This came up as a daily bugsmash target

    Since it's been 4+ years without movement I wonder if still hitting the issue? The code in question EntityReferenceFormatterBase:needsEntityLoad is the same but not sure if underlying issue got fixed with 4 years of updates.

    Leaving the tests tag as it will need test coverage if still an issue.

  • πŸ‡©πŸ‡ͺGermany mxh Offenburg

    Usually I close or update issues accordingly when I find a solution or it had been fixed in the meantime. This issue is too long ago to remember why I encountered it, so I probably just found yet another workaround and moved on. And since no one else has encountered the same issue so far, this may be closed as outdated.

Production build 0.71.5 2024