Problem/Motivation

  1. Create a new node with 3 paragraphs of the same type but different content and save it
  2. Edit the node and duplicate the first paragraph

Expected result
A new paragraph with the content from the first paragraph will be created.

Actual result
The content from the last paragraph will be used.

Proposed resolution

Fix the bug :)

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Primsi created an issue. See original summary.

primsi’s picture

Priority: Normal » Major
StatusFileSize
new1.15 KB

Initial patch, still missing tests. I think that the problem is that we are missing items in the user input array, which then messes up the value assigning part of form building.

Also bumping to major, because this can lead to data loss.

primsi’s picture

Status: Active » Needs review

Let's see first if we broke something.

Status: Needs review » Needs work

The last submitted patch, 2: duplicated_paragraphs-2887747-2.patch, failed testing. View results

primsi’s picture

Modified tests and removed the change about where the paragraph is inserted.

primsi’s picture

Status: Needs work » Needs review

The last submitted patch, 5: duplicated_paragraphs-2887747-5-TESTONLY.patch, failed testing. View results

The last submitted patch, 8: duplicated_paragraphs-2887747-8-TESTONLY.patch, failed testing. View results

miro_dietiker’s picture

I'll test this a bit on some production environment while working with content.

berdir’s picture

Priority: Major » Critical
Status: Needs review » Needs work
+++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
@@ -1196,7 +1196,13 @@ class ParagraphsWidget extends WidgetBase {
+
+    $user_input = $form_state->getUserInput();
+    $duplicate_input = $user_input[$field_name][$delta];
+    array_splice($user_input[$field_name], $delta + 1, 0, [$duplicate_input]);
+    $form_state->setUserInput($user_input);
+

@@ -1204,7 +1210,7 @@ class ParagraphsWidget extends WidgetBase {
 
-    array_splice($widget_state['paragraphs'], $delta + 1, 0, $paragraph);
+    array_splice($widget_state['paragraphs'], $delta +1 , 0, $paragraph);
 

this might conflict with other things that we've also soon.

the way this works for new paragraphs, no matter where they are placed exactly is to append them and then use original deltas to position them.

We might want to consider doing the same here..

We also noticed similar bugs when duplicating a paragraph and then moving it before save, we ended up with some paragraphs lost and others duplicated.

Also marking as critical due to that. It's just the experimental widget, sure, but we are using that in production and so might others. API stability is one thing, data stability another.

berdir’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
StatusFileSize
new7.1 KB
new5.76 KB

Uh, this was a ride :)

Had to change the test a bit to work with this approach, bu it is working quite well now.

Tried moving items around before and after duplicating (and both) and it seems to be quite stable.

We need a test that does something like this:

Create a node with 3 paragraphs, A, B, C. Then move C first, then duplicate C, so youhave CCAB, then move B to second, then save. then make sure you have CBCA.

berdir’s picture

Issue tags: -Needs tests +undefined
StatusFileSize
new7.37 KB
new5.99 KB

Expanded the test to cover that reorder stuff as well as deletions.

berdir’s picture

Issue tags: -undefined

  • Primsi committed 0475a7d on 8.x-1.x
    Issue #2887747 by Primsi, Berdir, miro_dietiker: Duplicated paragraphs...
primsi’s picture

Status: Needs review » Fixed

Ok, let's get this in. Thanks!

Status: Fixed » Closed (fixed)

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