Referenced files are cloned twice. Cloned parent entity references the wrong file.

Created on 3 August 2023, over 1 year ago
Updated 14 August 2024, 8 months ago

Problem/Motivation

When cloning an entity that references a File, the new option to also clone the referenced File is great - but doesn't quite work right. After submitting the form, the referenced File entity is cloned twice - one clone has the copied file URI, another clone has the original file URI. The parent entity references the second, incorrect File clone that points to the original URI.

Data before clone

Node field data                   file_managed
+-----+-----------------------+   +-----+------------------------------+
| nid | field_image_target_id |   | fid | filename | uri               |
+-----+-----------------------+   +-----+------------------------------+
|   1 |                     1 |   |   1 | test.jpg | public://test.jpg |
+-----+-----------------------+   +-----+------------------------------+


Data after clone - expected

Node field data                   file_managed
+-----+-----------------------+   +-----+-----------------------------------------+
| nid | field_image_target_id |   | fid | filename          | uri                 |
+-----+-----------------------+   +-----+-----------------------------------------+
|   1 |                     1 |   |   1 | test.jpg          | public://test.jpg   |
|   2 |                     2 |   |   2 | test.jpg - Cloned | public://test_0.jpg |
+-----+-----------------------+   +-----+-----------------------------------------+


Data after clone - actual

Node field data                   file_managed
+-----+-----------------------+   +-----+-----------------------------------------+
| nid | field_image_target_id |   | fid | filename          | uri                 |
+-----+-----------------------+   +-----+-----------------------------------------+
|   1 |                     1 |   |   1 | test.jpg          | public://test.jpg   |
|   2 |                     3 |   |   2 | test.jpg - Cloned | public://test_0.jpg |
+-----+-----------------------+   |   3 | test.jpg          | public://test.jpg   |
                                  +-----+-----------------------------------------+


Steps to reproduce

Create a node with a File or Image entity reference. Clone the node, including the referenced File.

Proposed resolution

ContentEntityCloneBase::cloneReferencedEntities() assumes that the $cloned_entity object will be modifed in-place by the call to cloneEntity(), which does not in fact happen because of how FileEntityClone::cloneEntity() works. Use the return value of cloneEntity() instead.

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024