getEntitiesToView($items, $langcode) as $delta => $entity) { if ($entity->id()) { $elements[$delta] = [ '#plain_text' => $entity->id(), // Create a cache tag entry for the referenced entity. In the case // that the referenced entity is deleted, the cache for referring // entities must be cleared. '#cache' => [ 'tags' => $entity->getCacheTags(), ], ]; } } return $elements; } }