Commit fc3381e1 authored by webchick's avatar webchick
Browse files

Issue #2403873 by larowlan: FileFormatterBase does not retain unsaved entities (files)

parent 3b53231a
......@@ -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;
}
}
......
......@@ -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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment