Skip to content

Commit 277ae50

Browse files
Issue #1483736 by stefan.r, bfcam, jrigby: field_attach_update deletes file fields (content & file) in entity regardless of if they are included in the entity object
1 parent d6ae9e6 commit 277ae50

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

modules/file/file.field.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ function file_field_insert($entity_type, $entity, $field, $instance, $langcode,
252252
* Checks for files that have been removed from the object.
253253
*/
254254
function file_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
255+
// Check whether the field is defined on the object.
256+
if (!isset($entity->{$field['field_name']})) {
257+
// We cannot check for removed files if the field is not defined.
258+
return;
259+
}
260+
255261
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
256262

257263
// On new revisions, all files are considered to be a new usage and no

modules/file/tests/file.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,15 @@ class FileFieldWidgetTestCase extends FileFieldTestCase {
474474
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
475475
$this->assertFileExists($node_file, 'New file saved to disk on node creation.');
476476

477+
// Test that running field_attach_update() leaves the file intact.
478+
$field = new stdClass();
479+
$field->type = $type_name;
480+
$field->nid = $nid;
481+
field_attach_update('node', $field);
482+
$node = node_load($nid);
483+
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
484+
$this->assertFileExists($node_file, 'New file still saved to disk on field update.');
485+
477486
// Ensure the file can be downloaded.
478487
$this->drupalGet(file_create_url($node_file->uri));
479488
$this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');

0 commit comments

Comments
 (0)