diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php index 2ecaaad..f53a162 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php @@ -58,13 +58,17 @@ public function prepareView(array $entities, $langcode, array &$items) { } } - // Iterate through the fieldable entities again to attach the loaded data. + // Iterate through the fieldable entities again to attach the loaded + // data. foreach ($entities as $id => $entity) { - + $rekey = FALSE; foreach ($items[$id] as $delta => $item) { // If we have a revision-ID, the key uses it as-well. $identifier = !empty($item['revision_id']) ? $item['target_id'] . ':' . $item['revision_id'] : $item['target_id']; if (!isset($target_entities[$identifier])) { + // The entity no longer exists, so remove the key. + $rekey = TRUE; + unset($items[$id][$delta]); continue; } @@ -81,6 +85,11 @@ public function prepareView(array $entities, $langcode, array &$items) { // Mark item as accessible. $items[$id][$delta]['access'] = TRUE; } + + if ($rekey) { + // Rekey the items array. + $items[$id] = array_values($items[$id]); + } } }