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.