diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 5772c95..4c29867 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -1082,8 +1082,9 @@ class InlineParagraphsWidget extends WidgetBase { /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */ $display = $widget_state['paragraphs'][$item['_original_delta']]['display']; - $display->extractFormValues($paragraphs_entity, $element[$item['_original_delta']]['subform'], $form_state); - + if ($widget_state['paragraphs'][$delta]['mode'] == 'edit') { + $display->extractFormValues($paragraphs_entity, $element[$item['_original_delta']]['subform'], $form_state); + } $paragraphs_entity->setNewRevision($new_revision); // A content entity form saves without any rebuild. It needs to set the // language to update it in case of language change. diff --git a/src/Tests/ParagraphsInlineEntityFormTest.php b/src/Tests/ParagraphsInlineEntityFormTest.php new file mode 100644 index 0000000..906f878 --- /dev/null +++ b/src/Tests/ParagraphsInlineEntityFormTest.php @@ -0,0 +1,87 @@ +addParagraphedContentType('article', 'field_paragraphs'); + $this->loginAsAdmin(['create article content', 'edit any article content']); + + // Create the paragraphs type simple. + $this->addParagraphsType('simple'); + + // Create a reference to an article. + $this->fieldUIAddNewField('admin/structure/paragraphs_type/simple', 'article', 'Article', 'field_ui:entity_reference:node', [ + 'settings[target_type]' => 'node', + 'cardinality' => 'number', + 'cardinality_number' => 1, + ], [ + 'required' => TRUE, + 'settings[handler_settings][target_bundles][article]' => TRUE + ]); + + // Enable IEF simple widget. + $this->drupalGet('admin/structure/paragraphs_type/simple/form-display'); + $edit = [ + 'fields[field_article][type]' => 'inline_entity_form_simple', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + // Set the paragraphs widget mode to preview. + $this->setParagraphsWidgetMode('article', 'field_paragraphs', 'preview'); + + // Create node with one paragraph. + $this->drupalGet('node/add/article'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_simple_add_more'); + + // Set the values and save. + $edit = [ + 'title[0][value]' => 'Dummy1', + 'field_paragraphs[0][subform][field_article][0][inline_entity_form][title][0][value]' => 'Dummy2', + ]; + $this->drupalPostForm(NULL, $edit, t('Save and publish')); + + // Go back into edit page. + $node = $this->getNodeByTitle('Dummy1'); + $this->drupalGet('node/' . $node->id() . '/edit'); + + // Try to open the previewed paragraph. + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit'); + } + + /** + * Sets the Paragraphs widget display mode. + * + * @param string $content_type + * Content type name where to set the widget mode. + * @param string $paragraphs_field + * Paragraphs field to change the mode. + * @param string $mode + * Mode to be set. ('closed', 'preview' or 'open'). + */ + protected function setParagraphsWidgetMode($content_type, $paragraphs_field, $mode) { + $this->drupalGet('admin/structure/types/manage/' . $content_type . '/form-display'); + $this->drupalPostAjaxForm(NULL, [], $paragraphs_field . '_settings_edit'); + $this->drupalPostForm(NULL, ['fields[' . $paragraphs_field . '][settings_edit_form][settings][edit_mode]' => $mode], t('Update')); + $this->drupalPostForm(NULL, [], 'Save'); + } +}