diff --git a/paragraphs.info.yml b/paragraphs.info.yml index 5b0e717..2702745 100644 --- a/paragraphs.info.yml +++ b/paragraphs.info.yml @@ -8,4 +8,5 @@ dependencies: - entity_reference_revisions test_dependencies: - diff - - replicate \ No newline at end of file + - replicate + - inline_entity_form \ No newline at end of file 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..7a23b7a --- /dev/null +++ b/src/Tests/ParagraphsInlineEntityFormTest.php @@ -0,0 +1,129 @@ +drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); + // Place the breadcrumb, tested in fieldUIAddNewField(). + $this->drupalPlaceBlock('local_tasks_block'); + $this->drupalPlaceBlock('local_actions_block'); + $this->drupalPlaceBlock('system_breadcrumb_block'); + } + + /** + * Tests the revision of paragraphs. + */ + public function testParagraphsIEFPreview() { + $admin_user = $this->drupalCreateUser(array( + 'administer nodes', + 'administer content types', + 'administer node fields', + 'administer node display', + 'administer paragraphs types', + 'administer paragraph fields', + 'administer paragraph form display', + 'administer node form display', + 'create article content', + 'edit any article content', + 'delete any article content', + )); + $this->drupalLogin($admin_user); + + $this->drupalGet('admin/structure/paragraphs_type'); + $this->clickLink(t('Add paragraphs type')); + // Create paragraph type. + $edit = array( + 'label' => 'Simple', + 'id' => 'simple', + ); + $this->drupalPostForm(NULL, $edit, t('Save and manage fields')); + + // Create a reference to an article. + $this->fieldUIAddNewField('admin/structure/paragraphs_type/simple', 'article', 'Article', 'field_ui:entity_reference:node', array( + 'settings[target_type]' => 'node', + 'cardinality' => 'number', + 'cardinality_number' => 1, + ), array( + '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')); + + // Create an article with paragraphs field. + static::fieldUIAddNewField('admin/structure/types/manage/article', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array( + 'settings[target_type]' => 'paragraph', + 'cardinality' => '-1', + ), array( + 'settings[handler_settings][target_bundles_drag_drop][simple][enabled]' => TRUE, + )); + + // Configure article fields. + $this->drupalGet('admin/structure/types/manage/article/form-display'); + $this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'entity_reference_paragraphs'), t('Save')); + + // Use paragraphs preview mode + $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_settings_edit'); + $edit = [ + 'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'preview', + ]; + $this->drupalPostForm(NULL, $edit, t('Update')); + $this->drupalPostForm(NULL, [], t('Save')); + + // Create node with one paragraph. + $this->drupalGet('node/add/article'); + $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_simple_add_more'); + + // Set the value of the node and the paragraph. + $edit = [ + 'title[0][value]' => 'Dummy1', + 'field_paragraphs[0][subform][field_article][0][inline_entity_form][title][0][value]' => 'Dummy2', + ]; + // Save the article. + $this->drupalPostForm(NULL, $edit, t('Save and publish')); + + // Go back into edit page + $this->drupalGet('node/2/edit'); + + // Try to open the previewed paragraph. + $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_0_edit'); + } +}