Cloning nested paragraph creates shared child reference

Created on 4 March 2025, 2 months ago

Problem/Motivation

On a new node creation, when cloning a paragraph containing nested paragraph child(s), the original and cloned paragraphs point to the same child entity after the node save.

Steps to reproduce

1. Go to https://simplytest.me
2. Launch a site using Paragraphs 8.x-1.x-dev
3. Create 2 new paragraph types, a) Text containing a text field and b) Section containing a paragraph reference field of Text
4. Update the Basic page content type to contain a Content field that's a paragraph reference field of Section
5. Add a new Basic page
6. Add some text to the first Section > Text, e.g. "Hello World"
7. Now click on the "..." of the Section and click "Duplicate"
8. Update the text of cloned Section > Text to something else, e.g. "Hello World2"
9. Save the node
10. Both text will display "Hello World2"

Proposed resolution

In this issue: https://www.drupal.org/project/paragraphs/issues/3495373 📌 Support duplicate hook Active
A new condition !$item->entity->isNew() was introduced to prevent nested paragraphs from being cloned in this scenario. However, I'm uncertain about the original intent behind this change, so I can't suggest a solution at this stage.

🐛 Bug report
Status

Active

Version

1.19

Component

Code

Created by

🇹🇼Taiwan peterwcm

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

Merge Requests

Comments & Activities

  • Issue created by @peterwcm
  • Pipeline finished with Success
    2 months ago
    Total: 352s
    #440640
  • 🇮🇳India arunsahijpal

    Hi @peterwcm,
    I've updated the createDuplicate method please check.

    Also @berdir pls check is this a good solution?

    Thanks,
    Arun

  • 🇹🇼Taiwan peterwcm

    Hi @arunsahijpal,

    Thanks for working on this. The patch fixes the issue, but it causes orphaned paragraphs to be created before users save the node. I think there could be issues with translations as well.

    I'm puzzled about the !$item->entity->isNew() condition, as it seemed to work fine without it before. Could we consider removing that condition instead? @berdir

    Regards

    Peter

  • Pipeline finished with Failed
    2 months ago
    Total: 639s
    #441719
  • 🇮🇳India arunsahijpal

    @peterwcm,

    I've updated the code and now no more orphaned paragraphs and everything saves when the node saves by referencing the entity directly instead of manually setting target_id.
    and I also think this condition !$item->entity->isNew() is not necessary.
    For the translation thing there is one limitation that - Paragraphs are referenced entities, not simple text fields.
    Drupal does not natively support translating entity reference fields.
    When I tried to translate it shows this error

    * (unsupported) Paragraphs fields do not support translation
    

    . Yo can checkout this link for more info .

    I think there is one tot check translation with Paragraphs Library module but I'm not sure.
    Could you pls check it, if it works then I think we can safely remove that condition.

  • 🇹🇼Taiwan peterwcm

    Hi @arunsahijpal,

    Thanks for addressing this so quickly.
    I just did some quick tests, and the latest patch has fixed the issues.

  • Status changed to RTBC about 16 hours ago
  • 🇵🇱Poland alorenc Wolsztyn, 🇵🇱

    alorenc changed the visibility of the branch 3510842-cloning-nested-paragraph to hidden.

  • 🇵🇱Poland alorenc Wolsztyn, 🇵🇱

    alorenc changed the visibility of the branch 3510842-cloning-nested-paragraph to active.

  • 🇨🇭Switzerland berdir Switzerland

    The check is in preparation for the new hook in Drupal 11.2 to avoid additional duplication. The changes remove the relevant todo and there is no test coverage for the bug that this seems to fix.

    We have fairly extensive duplication tests, if there is a bug then that should be covered by tests.

  • 🇵🇹Portugal dxvargas

    This problem is not happening when using drupal core 11.2 In this case, the \Drupal\paragraphs\Hook\EntityHooks::duplicate is doing the job.
    @peterwcm, @arunsahijpal can you please tell us if you're using a version of drupal prior to 11.2?

  • 🇵🇹Portugal dxvargas

    Still, I want to confirm that there is a bug when using Drupal core prior to 11.2 (when the new hook "entity_duplicate" is not run).

    It happens when we duplicate a new introduced paragraph (with nested paragraphs).
    In that case the new condition !$item->entity->isNew() is FALSE and the nested paragraphs are not duplicated. They end up being used in the original and in the duplicated paragraph.
    Sorry that I can't provide a test ATM.

    Just removing the new condition !$item->entity->isNew() fixes this.

  • 🇹🇼Taiwan peterwcm

    Hi @dxvargas,

    Yes, we are now using Drupal 10.4.6. I couldn't remember the version I tested when using simplytest.me, but looking at the options, it would also be prior to Drupal 11.

Production build 0.71.5 2024