Problem/Motivation

When deleting an entity, references to the deleted entity remain in entity reference fields (https://www.drupal.org/node/2723323).

jsonapi/src/Controller/EntityResource.php assumes that $field_item is an Entity but if the referenced entity is deleted,
we will get Error: Call to a member function id() on null in Drupal\jsonapi\Controller\EntityResource->getRelated().

foreach ($field_list as $field_item) {
   /* @var \Drupal\Core\Entity\EntityInterface $entity_item */
   $entity_item = $field_item->entity;
   $collection_data[$entity_item->id()] = static::getEntityAndAccess($entity_item);
   $cacheable_metadata->addCacheableDependency($entity_item);
}

Comments

apupiales created an issue. See original summary.

apupiales’s picture

StatusFileSize
new1.14 KB

This is a possible solution for this issue:

e0ipso’s picture

Status: Active » Needs review

This looks good to me. Triggering tests.

dawehner’s picture

+++ b/src/Controller/EntityResource.php
@@ -313,11 +313,10 @@ class EntityResource {
+    /* @var \Drupal\Core\Entity\EntityInterface $entity_item */
+    foreach ($field_list->referencedEntities() as $entity_item) {
+       $collection_data[$entity_item->id()] = static::getEntityAndAccess($entity_item);
+       $cacheable_metadata->addCacheableDependency($entity_item);

For clarity I'd call the variable $referenced_entity. It seems a bit more obvious what is going on ...

  • e0ipso committed 0dbe5c1 on 8.x-1.x authored by apupiales
    fix(Handler): Use only available reference items in related operations...
e0ipso’s picture

Status: Needs review » Fixed

This was fixed. I made some minor changes on commit.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.