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.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bgreco created an issue. See original summary.

bgreco’s picture

Status: Active » Needs review

bgreco’s picture

Title: Refrenced files are cloned twice. Cloned parent entity references the wrong file. » Referenced files are cloned twice. Cloned parent entity references the wrong file.
bgreco’s picture

Issue summary: View changes
s_leu’s picture

Patch is solving the problem outlined in the IS for me.

Rajeshreeputra made their first commit to this issue’s fork.

rajeshreeputra’s picture

Version: 2.0.0-beta4 » 2.x-dev
Status: Needs review » Fixed

Changes looks good, and works as well, merged. Release to follow shortly!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.