When a node with a multiple value file field contains items that are set to not be displayed, previewing changes to the node will remove all but the first undisplayed item from the field values.
Steps to reproduce in a fresh Drupal 7 installation:
- Add a file field to a content type.
- Set the number of values to unlimited.
- Enable Display field.
- Enable Files displayed by default.
- Log in as a regular user who can create content.
- Create a node and attach at least two files to the field. Save the node.
- Edit the node and uncheck the Display box for these files. (Or uncheck them when first creating the node. The result is the same.)
- Click Preview. All but the first of the file values will have disappeared from the list.
- If you save the node, it will save what the form says, so the other files will no longer be associated with the node (or at least the current revision.)
I suspect that this may be due to file_field_prepare_view() being called during the node preview, because that function simply removes non-displayed items from the entity. It might also be related to #1990818: node_preview() shouldn't call hook_field_load(), but I haven't had a chance to look at the actual code, so I'm really just guessing at the reasons behind this.
Marking this bug as major because it's very easy for content editors to corrupt data simply by previewing their changes to a node.
Comment | File | Size | Author |
---|---|---|---|
#36 | 2001308-preview_remove_files-36.patch | 3.67 KB | stefan.r |
#36 | interdiff.txt | 543 bytes | stefan.r |
#27 | 2001308-preview_remove_files-27.patch | 3.67 KB | stefan.r |
#27 | 2001308-preview_remove_files-27-testonly.patch | 1.36 KB | stefan.r |
#15 | test_file_preview-2001308-15.patch | 1.54 KB | marthinal |
Comments
Comment #1
David_Rothstein CreditAttribution: David_Rothstein commentedThanks for the very clear bug report!
Haven't fully thought through the consequences, but I wonder if just cloning the node object before previewing it (as in the attached patch) would be the right way to fix it? Seems to work at any rate.
It looks like this same code change would be needed in Drupal 8, but for whatever reason I can't reproduce the bug in Drupal 8 at the moment, so leaving at Drupal 7 for now.
Comment #2
micahw156Yes, even if this isn't the final solution, it solves the problem and is a viable workaround for now. Thanks!
Comment #3
Bill Choy CreditAttribution: Bill Choy commentedElevating to critical issue, because of data loss issue.
Comment #4
jlandfried CreditAttribution: jlandfried commentedAlso not sure if cloning the node is the best way to do this, but it solved the issue for me. I couldn't recreate the issue in d8 either.
Comment #5
David_Rothstein CreditAttribution: David_Rothstein commentedSince effectively the same code exists in Drupal 8, I think we need to fix this there first (even if for whatever reason the symptoms aren't as bad). It still seems to be the case in Drupal 8 that calling node_preview($node) will modify the provided $node object, and this could have other unexpected results.
Perhaps we need tests also?
Comment #6
David_Rothstein CreditAttribution: David_Rothstein commentedI'm curious if this issue is fixed by the patch in #1289336: Calling node_view for the same node with multiple view modes on the same page during node preview does not correctly attach fields also, by the way.
Comment #7
jthorson CreditAttribution: jthorson commentedDavid,
I confirmed that the fix from #1289336: Calling node_view for the same node with multiple view modes on the same page during node preview does not correctly attach fields does NOT resolve this issue (in D7).
Edit: And confirmed that the patch from #1 does.
Comment #8
twardnw CreditAttribution: twardnw commentedI have also tested the patch in #1 and had success resolving this issue in D7
Comment #9
helmo CreditAttribution: helmo commentedHere's a patch for D8 for testing...
I've also removed the unset line ... the whole cloned object is no longer used...
Comment #10
webchickNeeds tests to check that the regression is fixed.
Comment #11
tvn CreditAttribution: tvn commentedAdding tag since this issue affects D7 Drupal.org, we'll have to live with a patch till it's in core.
Comment #12
tvn CreditAttribution: tvn commented.
Comment #13
swentel CreditAttribution: swentel commentedTried reproducing this on D8, but works fine, so I guess we don't need the code here, but only tests for D8 then ?
Comment #14
catchYep. Re-titling, leaving at critical since this is still nasty for 7.x / d.o
Comment #15
marthinal CreditAttribution: marthinal commentedPossible test attached.
Comment #16
marthinal CreditAttribution: marthinal commentedComment #18
marthinal CreditAttribution: marthinal commented15: test_file_preview-2001308-15.patch queued for re-testing.
Comment #19
helmo CreditAttribution: helmo commentedD8 test looks OK.
Lets get this in and move on the D7 fix which is also ready.
Comment #20
webchickCommitted and pushed to 8.x, thanks!
Marking down to 7.x, and needs work since we need a patch that combines the fix + the test for D7.
Comment #21
mgifford1: node-preview-2001308-1.patch queued for re-testing.
Comment #22
mgifford@David_Rothstein patch in #1 still applies.
I was able to confirm that it is still a bug and that the patch fixes it.
As @webchick says though, we need tests for D7.
Comment #23
drummComment #24
micahw156We've been using the patch from #1 in all of our Drupal 7 builds since it was first posted over a year ago. It solves the problem for us, and we still break content in the rare instances when we forget to reapply this when updating core.
We include this patch in all of our drush make files, and then of course have to reapply it every time we update core. Yes, it probably needs tests but it would be incredibly nice to just have this patch committed and be done with it.
If @drumm's status change on this implies the possibility of moving forward without adding tests, then yes, RTBC.
Comment #25
drummAdding tests or not is not up to me. That's a question for David_Rothstein.
We have been running #1 on Drupal.org for awhile, along with #1289336: Calling node_view for the same node with multiple view modes on the same page during node preview does not correctly attach fields. That looks likely to be be related, but I don't know the internals well enough to comment credibly.
Comment #26
webchickTo clarify, the tests are already written and are in the D8 patch, we just need a single unified patch for D7 that contains both the fix and the tests.
Comment #27
stefan.r CreditAttribution: stefan.r commentedComment #28
stefan.r CreditAttribution: stefan.r commentedThis patch combines the fix in #1 and the test as requested by @webchick. Patch is red with test only and green with fix+test so assuming this should be RTBC soon enough.
Comment #29
dcam CreditAttribution: dcam commentedCan we get this comment's grammar fixed? We'll have to do a follow-up to fix it in D8 also.
Otherwise the test looks good. I don't know why Testbot ignored the tests-only patch, so I ran the test locally and the second assertion did fail.
Comment #30
dcam CreditAttribution: dcam commentedI decided to go ahead and RTBC #27. Maybe @David_Rothstein can fix the comment on commit. I'm pretty sure it's supposed to say
Test that fields appear as expected during the preview.
If there's a chance that it can still get into 7.33 then it seems a shame to hold up a critical issue because of a comment.Comment #32
David_Rothstein CreditAttribution: David_Rothstein commentedHm, I could, although I am also not sure what the
'cardinality' => FIELD_CARDINALITY_UNLIMITED
change is doing in this patch...This was RTBC pretty late and I think I'm running out of steam to get this into 7.33.... sorry. Also, the bug was never fixed in Drupal 8? See my comment above:
The Drupal 8 code in question may not exist anymore though.. I'm not sure.
Comment #33
dcam CreditAttribution: dcam commentedOh well. It was worth a shot.
In #13 @swentel said it couldn't be reproduced in D8 anymore.
Comment #34
stefan.r CreditAttribution: stefan.r commentedThe unlimited cardinality was a backport from the D8 test, I assume this is just in order to add multiple files?
If I look at that test the comment meant to say during the preview as the assertion happens after clicking the preview button.
Comment #35
David_Rothstein CreditAttribution: David_Rothstein commentedOh, oops - sorry!... for some reason when I read that FIELD_CARDINALITY_UNLIMITED line in the patch I didn't think it was part of the test but rather thought it was in a completely different file. I have no idea why I thought that.
The comments above about not being able to reproduce on Drupal 8 were all about the specific steps to reproduce in this issue, at least as far as I can tell. But the code was still there, and calling node_preview($node) could still change the node rather than simply returning the preview HTML. However, I looked at the code now and I think as of a couple months ago it's all gone from Drupal 8, since #1510544: Allow to preview content in an actual live environment changed how node previews work entirely (they now happen at their own URL).
So I think we can consider this RTBC for Drupal 7 after all (minus the code comment fix which can be done on commit).
Comment #36
stefan.r CreditAttribution: stefan.r commentedGood! Here's the comment fix so you guys don't have to do it on commit :)
Comment #37
dcam CreditAttribution: dcam commentedI'm thinking it was because you were looking at it in the very early morning hours at the end of a marathon session of testing and committing issues. I certainly wouldn't blame you for being mentally exhausted. ;)
Thanks @stefan.r! #36 looks good to me. RTBC.
Comment #40
stefan.r CreditAttribution: stefan.r commentedComment #43
helmo CreditAttribution: helmo commentedreviewed again... works as expected.
Comment #47
David_Rothstein CreditAttribution: David_Rothstein commentedLooks like a testbot fluke.
Comment #48
David_Rothstein CreditAttribution: David_Rothstein commentedCommitted to 7.x - thanks!