diff --git a/core/modules/file/src/Entity/File.php b/core/modules/file/src/Entity/File.php index 0c34871f51d..6fd0cc758ae 100644 --- a/core/modules/file/src/Entity/File.php +++ b/core/modules/file/src/Entity/File.php @@ -186,7 +186,11 @@ class File extends ContentEntityBase implements FileInterface { public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); - $this->setSize(filesize($this->getFileUri())); + // The file itself might not exist or be available right now. + $uri = $this->getFileUri(); + if ($size = @filesize($uri)) { + $this->setSize($size); + } } /** diff --git a/core/modules/file/src/Tests/FileManagedFileElementTest.php b/core/modules/file/src/Tests/FileManagedFileElementTest.php index f5d5be39489..07102a04df9 100644 --- a/core/modules/file/src/Tests/FileManagedFileElementTest.php +++ b/core/modules/file/src/Tests/FileManagedFileElementTest.php @@ -171,4 +171,27 @@ class FileManagedFileElementTest extends FileFieldTestBase { $this->assertRaw('The file referenced by the Managed file & butter field does not exist.'); } + /** + * Ensure a file entity can be saved when the file does not exist on disk. + */ + public function testFileRemovedFromDisk() { + $this->drupalGet('file/test/1/0/1'); + $test_file = $this->getTestFile('text'); + $file_field_name = 'files[nested_file][]'; + + $edit = [$file_field_name => drupal_realpath($test_file->getFileUri())]; + $this->drupalPostForm(NULL, $edit, t('Upload')); + $this->drupalPostForm(NULL, array(), t('Save')); + + $fid = $this->getLastFileId(); + /** @var $file \Drupal\file\FileInterface */ + $file = $this->container->get('entity_type.manager')->getStorage('file')->load($fid); + $file->setPermanent(); + $file->save(); + $this->assertTrue(file_unmanaged_delete($file->getFileUri())); + $file->save(); + $this->assertTrue($file->isPermanent()); + $file->delete(); + } + }