diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index f7871f0..bb83146 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -884,7 +884,7 @@ class InlineParagraphsWidget extends WidgetBase { '#markup' => $this->t('No @title added yet.', ['@title' => $this->getSetting('title')]), '#prefix' => '', '#suffix' => '', - ], + ] ], ]; @@ -1254,15 +1254,7 @@ class InlineParagraphsWidget extends WidgetBase { $field_name = $this->fieldDefinition->getName(); $widget_state = static::getWidgetState($elements['#field_parents'], $field_name, $form_state); - $remove_mode_item_count = 0; - if (isset($widget_state['paragraphs'])) { - foreach ($widget_state['paragraphs'] as $paragraph) { - if ($paragraph['mode'] == 'remove') { - $remove_mode_item_count++; - } - } - } - + $remove_mode_item_count = $this->getNumberOfParagraphsInMode($widget_state, 'remove'); $non_remove_mode_item_count = $widget_state['real_item_count'] - $remove_mode_item_count ; if ($elements['#required'] && $non_remove_mode_item_count < 1) { @@ -1437,6 +1429,32 @@ class InlineParagraphsWidget extends WidgetBase { } /** + * Counts the number of paragraphs in a certain mode in a form substructure. + * + * @param array $widget_state + * The widget state for the form substructure containing information about + * the paragraphs within. + * @param string $mode + * The mode to look for. + * @return int + * The number of paragraphs is the given mode. + */ + protected function getNumberOfParagraphsInMode($widget_state, $mode) { + if (!isset($widget_state['paragraphs'])) { + return 0; + } + + $paragraphs_count = 0; + foreach ($widget_state['paragraphs'] as $paragraph) { + if ($paragraph['mode'] == $mode) { + $paragraphs_count++; + } + } + + return $paragraphs_count; + } + + /** * {@inheritdoc} */ public static function isApplicable(FieldDefinitionInterface $field_definition) { diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php index 56fea20..c639610 100644 --- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -1307,15 +1307,7 @@ class ParagraphsWidget extends WidgetBase { $field_name = $this->fieldDefinition->getName(); $widget_state = static::getWidgetState($elements['#field_parents'], $field_name, $form_state); - $remove_mode_item_count = 0; - if (isset($widget_state['paragraphs'])) { - foreach ($widget_state['paragraphs'] as $paragraph) { - if ($paragraph['mode'] == 'remove') { - $remove_mode_item_count++; - } - } - } - + $remove_mode_item_count = $this->getNumberOfParagraphsInMode($widget_state, 'remove'); $non_remove_mode_item_count = $widget_state['real_item_count'] - $remove_mode_item_count ; if ($elements['#required'] && $non_remove_mode_item_count < 1) { @@ -1505,6 +1497,32 @@ class ParagraphsWidget extends WidgetBase { } /** + * Counts the number of paragraphs in a certain mode in a form substructure. + * + * @param array $widget_state + * The widget state for the form substructure containing information about + * the paragraphs within. + * @param string $mode + * The mode to look for. + * @return int + * The number of paragraphs is the given mode. + */ + protected function getNumberOfParagraphsInMode($widget_state, $mode) { + if (!isset($widget_state['paragraphs'])) { + return 0; + } + + $paragraphs_count = 0; + foreach ($widget_state['paragraphs'] as $paragraph) { + if ($paragraph['mode'] == $mode) { + $paragraphs_count++; + } + } + + return $paragraphs_count; + } + + /** * {@inheritdoc} */ public static function isApplicable(FieldDefinitionInterface $field_definition) { diff --git a/src/Tests/Classic/ParagraphsUiTest.php b/src/Tests/Classic/ParagraphsUiTest.php index 253fa00..3788941 100644 --- a/src/Tests/Classic/ParagraphsUiTest.php +++ b/src/Tests/Classic/ParagraphsUiTest.php @@ -41,7 +41,8 @@ class ParagraphsUiTest extends ParagraphsTestBase { 'label' => $field_title, 'field_name' => 'content', ]; - $this->drupalPostForm('admin/structure/types/manage/paragraphed_content_demo/fields/add-field', $edit, t('Save and continue')); + $this->drupalGet('admin/structure/types/manage/paragraphed_content_demo/fields/add-field'); + $this->drupalPostForm(NULL, $edit, t('Save and continue')); $this->drupalPostForm(NULL, [], t('Save field settings')); $edit = [ 'required' => TRUE, diff --git a/src/Tests/Experimental/ParagraphsExperimentalUiTest.php b/src/Tests/Experimental/ParagraphsExperimentalUiTest.php index c798380..1c9239f 100644 --- a/src/Tests/Experimental/ParagraphsExperimentalUiTest.php +++ b/src/Tests/Experimental/ParagraphsExperimentalUiTest.php @@ -41,7 +41,8 @@ class ParagraphsExperimentalUiTest extends ParagraphsExperimentalTestBase { 'label' => $field_title, 'field_name' => 'content', ]; - $this->drupalPostForm('admin/structure/types/manage/paragraphed_content_demo/fields/add-field', $edit, t('Save and continue')); + $this->drupalGet('admin/structure/types/manage/paragraphed_content_demo/fields/add-field'); + $this->drupalPostForm(NULL, $edit, t('Save and continue')); $this->drupalPostForm(NULL, [], t('Save field settings')); $edit = [ 'required' => TRUE,