diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php index 3c45fe4..8693d3d 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php @@ -33,9 +33,10 @@ public function prepareView(array $entities_items) { $files = file_load_multiple($fids); foreach ($entities_items as $items) { + /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item */ foreach ($items as $item) { // If the file does not exist, mark the entire item as empty. - if (!empty($item->target_id)) { + if (!empty($item->target_id) && !$item->hasNewEntity()) { $item->entity = isset($files[$item->target_id]) ? $files[$item->target_id] : NULL; } } diff --git a/core/modules/file/src/Tests/FileItemTest.php b/core/modules/file/src/Tests/FileItemTest.php index f34d2f8..2c5c11c 100644 --- a/core/modules/file/src/Tests/FileItemTest.php +++ b/core/modules/file/src/Tests/FileItemTest.php @@ -100,6 +100,26 @@ public function testFileItem() { $entity = entity_create('entity_test'); $entity->file_test->generateSampleItems(); $this->entityValidateAndSave($entity); + + // Make sure the computed files reflects updates to the file. + file_put_contents('public://example-3.txt', $this->randomMachineName()); + // Test unsaved file entity. + $file3 = entity_create('file', array( + 'uri' => 'public://example-3.txt', + )); + $display = entity_get_display('entity_test', 'entity_test', 'default'); + $display->setComponent('file_test', [ + 'label' => 'above', + 'type' => 'file_default', + 'weight' => 1, + ])->save(); + $entity = entity_create('entity_test'); + $entity->file_test = array('entity' => $file3); + $uri = $file3->getFileUri(); + $output = entity_view($entity, 'default'); + drupal_render($output); + $this->assertTrue(!empty($entity->file_test->entity)); + $this->assertEqual($entity->file_test->entity->getFileUri(), $uri); } }