diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 0c2e028..305e30a 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -231,6 +231,8 @@ class InlineParagraphsWidget extends WidgetBase { $info = []; $paragraphs_entity = NULL; + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */ + $display = NULL; $host = $items->getEntity(); $widget_state = static::getWidgetState($parents, $field_name, $form_state); @@ -278,26 +280,33 @@ class InlineParagraphsWidget extends WidgetBase { $item_mode = $default_edit_mode; } - if ($item_mode == 'closed' && $paragraphs_entity && - isset($widget_state['paragraphs'][$delta]['display'])) { - // Validate closed paragraphs and expand if needed. + // Initialize the form display. + if ($paragraphs_entity) { + $display = EntityFormDisplay::collectRenderDisplay($paragraphs_entity, $this->getSetting('form_display_mode')); + } + + // Validate closed paragraphs and expand if needed. + if ($item_mode == 'closed' && $paragraphs_entity && $display) { // @todo Consider recursion. // @todo Is this needed anymore once the collapse button validate the // subform fields? - /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */ - $display = $widget_state['paragraphs'][$delta]['display']; $violations = $paragraphs_entity->validate(); $violations->filterByFieldAccess(); if ($violations->count() > 0) { + $violations_excluded_fields = []; foreach ($violations->getFieldNames() as $field_name) { // Only show violations for fields that actually appear in the form, // and let the widget assign the violations to the correct form // elements. if (!$display->getRenderer($field_name)) { - $violations->filterByFields([$field_name]); + $violations_excluded_fields[] = $field_name; } } + if ($violations_excluded_fields) { + $violations->filterByFields($violations_excluded_fields); + } + // If violations for this form mode ... if ($violations->count() > 0) { // Switch mode to 'edit' so that the subform is built. @@ -643,8 +652,6 @@ class InlineParagraphsWidget extends WidgetBase { } } - $display = EntityFormDisplay::collectRenderDisplay($paragraphs_entity, $this->getSetting('form_display_mode')); - // @todo Remove as part of https://www.drupal.org/node/2640056 if (\Drupal::moduleHandler()->moduleExists('field_group')) { $context = [ diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php index ea9b551..104960a 100644 --- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -226,6 +226,8 @@ class ParagraphsWidget extends WidgetBase { /** @var \Drupal\paragraphs\Entity\Paragraph $paragraphs_entity */ $paragraphs_entity = NULL; + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */ + $display = NULL; $host = $items->getEntity(); $widget_state = static::getWidgetState($parents, $field_name, $form_state); @@ -269,26 +271,33 @@ class ParagraphsWidget extends WidgetBase { $item_mode = 'edit'; } - if ($item_mode == 'closed' && $paragraphs_entity && - isset($widget_state['paragraphs'][$delta]['display'])) { - // Validate closed paragraphs and expand if needed. + // Initialize the form display. + if ($paragraphs_entity) { + $display = EntityFormDisplay::collectRenderDisplay($paragraphs_entity, $this->getSetting('form_display_mode')); + } + + // Validate closed paragraphs and expand if needed. + if ($item_mode == 'closed' && $paragraphs_entity && $display) { // @todo Consider recursion. // @todo Is this needed anymore once the collapse button validate the // subform fields? - /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */ - $display = $widget_state['paragraphs'][$delta]['display']; $violations = $paragraphs_entity->validate(); $violations->filterByFieldAccess(); if ($violations->count() > 0) { + $violations_excluded_fields = []; foreach ($violations->getFieldNames() as $field_name) { // Only show violations for fields that actually appear in the form, // and let the widget assign the violations to the correct form // elements. if (!$display->getRenderer($field_name)) { - $violations->filterByFields([$field_name]); + $violations_excluded_fields[] = $field_name; } } + if ($violations_excluded_fields) { + $violations->filterByFields($violations_excluded_fields); + } + // If violations for this form mode ... if ($violations->count() > 0) { // Switch mode to 'edit' so that the subform is built. @@ -582,8 +591,6 @@ class ParagraphsWidget extends WidgetBase { } } - $display = EntityFormDisplay::collectRenderDisplay($paragraphs_entity, $this->getSetting('form_display_mode')); - // @todo Remove as part of https://www.drupal.org/node/2640056 if (\Drupal::moduleHandler()->moduleExists('field_group')) { $context = [ diff --git a/src/Tests/Classic/ParagraphsAdministrationTest.php b/src/Tests/Classic/ParagraphsAdministrationTest.php index 25ecf74..c4a3d4f 100644 --- a/src/Tests/Classic/ParagraphsAdministrationTest.php +++ b/src/Tests/Classic/ParagraphsAdministrationTest.php @@ -456,7 +456,6 @@ class ParagraphsAdministrationTest extends ParagraphsTestBase { ]); $this->clickLink(t('Manage form display')); $this->drupalPostForm(NULL, array('fields[field_entity_reference][type]' => 'entity_reference_autocomplete'), t('Save')); - $this->drupalPostForm(NULL, [], 'Save'); $node = $this->drupalGetNodeByTitle('Nested twins'); diff --git a/src/Tests/Experimental/ParagraphsExperimentalAdministrationTest.php b/src/Tests/Experimental/ParagraphsExperimentalAdministrationTest.php index ad78e6a..c2b2d02 100644 --- a/src/Tests/Experimental/ParagraphsExperimentalAdministrationTest.php +++ b/src/Tests/Experimental/ParagraphsExperimentalAdministrationTest.php @@ -452,7 +452,6 @@ class ParagraphsExperimentalAdministrationTest extends ParagraphsExperimentalTes ]); $this->clickLink(t('Manage form display')); $this->drupalPostForm(NULL, array('fields[field_entity_reference][type]' => 'entity_reference_autocomplete'), t('Save')); - $this->drupalPostForm(NULL, [], 'Save'); $node = $this->drupalGetNodeByTitle('Nested twins');