diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php index edb24b9..4300f05 100644 --- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -1624,13 +1624,20 @@ class ParagraphsWidget extends WidgetBase { $field_name = $this->fieldDefinition->getName(); $field_title = $this->fieldDefinition->getLabel(); $setting_title = $this->getSetting('title'); + $select_options = $this->getAccessibleOptions(); $add_more_elements['add_more_select'] = [ '#type' => 'select', - '#options' => $this->getAccessibleOptions(), + '#options' => $select_options, '#title' => $this->t('@title type', ['@title' => $setting_title]), '#label_display' => 'hidden', ]; + // Do not present the select element if only one option is available. + if (count($select_options) === 1) { + $add_more_elements['add_more_select']['#type'] = 'value'; + $add_more_elements['add_more_select']['#value'] = key($select_options); + } + $text = $this->t('Add @title', ['@title' => $setting_title]); if ($this->realItemCount > 0) { diff --git a/tests/src/Functional/ParagraphsExperimentalUiTest.php b/tests/src/Functional/ParagraphsExperimentalUiTest.php index f4d24d2..b21e2c0 100644 --- a/tests/src/Functional/ParagraphsExperimentalUiTest.php +++ b/tests/src/Functional/ParagraphsExperimentalUiTest.php @@ -98,4 +98,40 @@ class ParagraphsExperimentalUiTest extends BrowserTestBase { $this->assertSession()->responseContains('class="paragraphs-description paragraphs-collapsed-description">
This is a title'); } + /** + * Tests 'Select list' add mode logic. + */ + public function testAddModeSelect() { + $this->loginAsAdmin(); + $this->addParagraphedContentType('paragraphed_test', 'paragraphs'); + + $this->addParagraphsType('test_paragraph'); + $this->addParagraphsType('text'); + $this->addFieldtoParagraphType('text', 'field_text_demo', 'text'); + $settings = [ + 'add_mode' => 'select', + 'edit_mode' => 'closed', + 'closed_mode' => 'summary', + ]; + $this->setParagraphsWidgetSettings('paragraphed_test', 'paragraphs', $settings, 'paragraphs'); + $this->drupalGet('node/add/paragraphed_test'); + $this->assertSession()->selectExists('paragraphs[add_more][add_more_select]'); + + $edit = [ + 'settings[handler_settings][negate]' => 0, + 'settings[handler_settings][target_bundles_drag_drop][text][enabled]' => 1, + ]; + $this->drupalPostForm('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs', $edit, 'Save settings'); + + $this->drupalGet('node/add/paragraphed_test'); + $this->assertSession()->fieldNotExists('paragraphs[add_more][add_more_select]'); + $this->getSession()->getPage()->findButton('paragraphs_add_more')->press(); + $edit = [ + 'title[0][value]' => 'Demo text title', + 'paragraphs[0][subform][field_text_demo][0][value]' => 'Demo text for the detail page', + ]; + $this->drupalPostForm(NULL, $edit, 'Save'); + $this->assertSession()->pageTextContains('Demo text for the detail page'); + } + }