Problem/Motivation
All 'file' field usages belonging to an entity are deleted when an entity translation is deleted. The deletion of an entity translation should decrement the file usage only vir file fields of contained by that translation.
Steps to reproduce:
- Create a content type with an image field
- Enable translation for your content type and image field
- Create a new node in English, (including uploading a file to the image field)
- Add a translation of the node (Spanish, let's say). You can leave the same image in place, or use a different one, shouldn't matter.
- Delete just the Spanish translation of the node.
- Observe that the file_usage entry for the referenced file has been completely wiped out, despite the file still being in use by the English version. The status flag in file_managed also gets set to 0 making it eligible for cleanup.
This happens because FileFieldItemList::delete() is not aware of entity translations. Entity translation deletion is piped throughFileFieldItemList::delete() as a normal entity deletion. The bug is revealed by the test-only patch.
Because this bug leads to data loss, is marked as Critical.
Proposed resolution
Fix it.
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#39 | interdiff.txt | 3.31 KB | claudiu.cristea |
#39 | 2787187-39-8.2-3.x.patch | 5.19 KB | claudiu.cristea |
#39 | 2787187-39-8.1.x.patch | 3.55 KB | claudiu.cristea |
Comments
Comment #2
azinck CreditAttribution: azinck commentedComment #3
grahlSeeing this as well, due to the data loss which silently occurs, this is at least major if not critical.
Comment #4
azinck CreditAttribution: azinck commentedYeah, I'm going to bump to critical.
Comment #5
xjmYowza. Thank you for reporting this.
Comment #6
claudiu.cristeaI'm writing a test.
Comment #7
grahlAttached is a patch which fixes my immediate problem.
With that check, loosely adapted from the postSave() function in place my translation only gets a decrement in file_usage of 1 instead of all. Though I might have queried the entity api incorrectly there since the deletion of the original doesn't actually seem to force the other half of the if statement.
The latter shouldn't be a big issue if the counter would not increment incorrectly. I am consistently seeing a count of 3 when a translation is added to the original instead of 2 (or is that intentional?).
Comment #8
claudiu.cristeaYepp, this test reveals the bug.
Comment #9
claudiu.cristeaComment #10
claudiu.cristeaAnd the patch (inspired from #7).
Comment #11
azinck CreditAttribution: azinck commentedSeems to me that we need to actually count up all the usages by all of the revisions of this entity in the given language. I don't think #10 is going to be sufficient. See #2639352: File records, files themselves lost in translation
Comment #14
claudiu.cristea@azinck, you're right but I think you are referring to #1239558: Deleting an entity with revisions and file/image field does not release file usage of non-default revisions, causing files to linger. Isn't so?
Comment #15
azinck CreditAttribution: azinck commentedYes, my mistake. Copy pasted wrong issue twice! Thank you.
Comment #16
claudiu.cristeaFixed the IS.
Comment #17
Mile23Given #11 and #14 I'm setting this to 'needs work.'
Comment #18
claudiu.cristea@Mile23, can you, please, be more specific? I don't think something else needs to be done here. Removing usages added by revisions is fixed in #1239558: Deleting an entity with revisions and file/image field does not release file usage of non-default revisions, causing files to linger, not here. As I recall is fixed by deleting the revisions and that is piped thorough \Drupal\file\Plugin\Field\FieldType\FileFieldItemList::deleteRevision.
Comment #19
xjm@catch, @effulgentsia, @alexpott, @Cottser, and I agreed that this is definitely a critical bug because of the data loss issue.
Comment #20
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedOverall looks great to me, but:
While this is pretty clever casting logic, it would be better to write this explicitly, as it is very hard to understand on a first glance.
Comment #21
claudiu.cristea@Fabianx, thank you for review.
Made it more explicit.
Comment #22
dawehnerCouldn't you use
isDefaultTranslation
actually?Comment #23
claudiu.cristeaSure, forgot that method.
Comment #24
claudiu.cristeaImproved the comment.
Comment #25
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedSweet, much better now!
Comment #26
catchCommitted/pushed to all three 8.x branches, thanks!
Comment #30
Gábor HojtsyYay, thanks!
Comment #34
catchTest failed in HEAD, reverted and sending for re-test on all three branches.
Comment #37
claudiu.cristeaThis is amazing. Those 2 tests are passing now in 8.2.x and 8.3.x HEAD because of the combined effect of two bugs that are compensating each other: this one and #1239558: Deleting an entity with revisions and file/image field does not release file usage of non-default revisions, causing files to linger. This bug releases too many file usages while the other wrongly add them back making, together, a zero balance. But because #1239558: Deleting an entity with revisions and file/image field does not release file usage of non-default revisions, causing files to linger is not yet committed, the effects of the 2 bugs are not compensating anymore.
I propose to temporarily get rid about that failed test till #1239558: Deleting an entity with revisions and file/image field does not release file usage of non-default revisions, causing files to linger. In that patch we need to restore the lines. See the interdiff for details.
The behavior has changed after #2490136: Enable revisions by default has been committed.
Comment #38
claudiu.cristeaBack to RTBC. As discussed with @catch on IRC we can live with the temporary solution until #1239558: Deleting an entity with revisions and file/image field does not release file usage of non-default revisions, causing files to linger.
Comment #39
claudiu.cristeaAnother approach suggested by @catch: making the node types from tests not adding new revisions by default.
Comment #40
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedBack to RTBC, looks good to me.
Comment #44
catchAlways fun when there are bugs that cancel each other out.
Committed/pushed to all three 8.x branches, thanks!
Comment #46
Wim LeersThis accidentally introduced a new critical: #2810355: $entity->isDefaultTranslation() behaves incorrectly when changing default translation, causing file/image field usage to be set to zero, causing files to be deleted, because
$entity->isDefaultTranslation()
is broken.Comment #47
Wim LeersThis also broke the case of when file A is used in translation X and file B is used in translation Y. When you delete the non-default translation, if that used a different file than the default translation, its usage will never reach zero.
Reopening to ensure a follow-up is created for that.
Comment #48
dawehnerI don't think though that the non reaching zero problem is a critical issue.
Comment #49
catchNow that #2801777: Give users the option to prevent drupal from automatically marking unused files as temporary is in, this no longer results in unexpected file deletion. #2821423: Dealing with unexpected file deletion due to incorrect file usage is open as a critical meta-issue to discuss the broader approach on this. So I'm going to downgrade this to major after discussion with @xjm and @cilefen, since we feel effort would be better spent on replacing the file_usage system at this point. We can still fix this bug of course as a major.
Comment #50
joseph.olstadI encountered issues when relying on file usage data in a cleanup job. I discovered that important files that were still 'in use' were deleted.
There is a related fix/patch that has tests and is passing tests.
see #2810355-61: $entity->isDefaultTranslation() behaves incorrectly when changing default translation, causing file/image field usage to be set to zero, causing files to be deleted
Appreciate community and core maintainers assistance to get this patch to where it needs to be in order for it to be accepted.
Thanks,
Comment #56
catch#47 re-opened this for a follow-up, I think that follow-up is now #3364744: Add a reliable entity-usage system to core so marking fixed again.