diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php index 60e06e6..9e7dc2f 100644 --- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -178,6 +178,11 @@ class ParagraphsWidget extends WidgetBase { '#options' => $this->getSettingOptions('autocollapse'), '#default_value' => $this->getSetting('autocollapse'), '#required' => TRUE, + '#states' => [ + 'visible' => [ + 'select[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][edit_mode]"]' => ['value' => 'closed'], + ], + ], ]; $elements['add_mode'] = array( @@ -277,12 +282,14 @@ class ParagraphsWidget extends WidgetBase { $edit_mode = $this->getSettingOptions('edit_mode')[$this->getSetting('edit_mode')]; $closed_mode = $this->getSettingOptions('closed_mode')[$this->getSetting('closed_mode')]; - $autocollapse = $this->getSettingOptions('autocollapse')[$this->getSetting('autocollapse')]; $add_mode = $this->getSettingOptions('add_mode')[$this->getSetting('add_mode')]; $summary[] = $this->t('Edit mode: @edit_mode', ['@edit_mode' => $edit_mode]); $summary[] = $this->t('Closed mode: @closed_mode', ['@closed_mode' => $closed_mode]); - $summary[] = $this->t('Autocollapse: @autocollapse', ['@autocollapse' => $autocollapse]); + if ($this->getSetting('edit_mode') == 'closed') { + $autocollapse = $this->getSettingOptions('autocollapse')[$this->getSetting('autocollapse')]; + $summary[] = $this->t('Autocollapse: @autocollapse', ['@autocollapse' => $autocollapse]); + } $summary[] = $this->t('Add mode: @add_mode', ['@add_mode' => $add_mode]); $summary[] = $this->t('Form display mode: @form_display_mode', [ @@ -761,6 +768,7 @@ class ParagraphsWidget extends WidgetBase { $widget_state['paragraphs'][$delta]['mode'] = $item_mode; $widget_state['closed_mode'] = $closed_mode_setting; $widget_state['autocollapse'] = $autocollapse_setting; + $widget_state['autocollapse_default'] = $this->getSetting('autocollapse'); static::setWidgetState($parents, $field_name, $form_state, $widget_state); } @@ -2417,12 +2425,13 @@ class ParagraphsWidget extends WidgetBase { } } - // Disable autocollapse when editing all and enable it when closing all. - if ($submit['button']['#paragraphs_mode'] === 'edit') { - $submit['widget_state']['autocollapse'] = 'none'; - } - elseif ($submit['button']['#paragraphs_mode'] === 'closed') { - $submit['widget_state']['autocollapse'] = 'all'; + if ($submit['widget_state']['autocollapse_default'] == 'all') { + if ($submit['button']['#paragraphs_mode'] === 'edit') { + $submit['widget_state']['autocollapse'] = 'none'; + } + elseif ($submit['button']['#paragraphs_mode'] === 'closed') { + $submit['widget_state']['autocollapse'] = 'all'; + } } static::setWidgetState($submit['parents'], $submit['field_name'], $form_state, $submit['widget_state']); diff --git a/tests/src/Functional/ParagraphsExperimentalWidgetButtonsTest.php b/tests/src/Functional/ParagraphsExperimentalWidgetButtonsTest.php index c892173..e4d504b 100644 --- a/tests/src/Functional/ParagraphsExperimentalWidgetButtonsTest.php +++ b/tests/src/Functional/ParagraphsExperimentalWidgetButtonsTest.php @@ -61,8 +61,9 @@ class ParagraphsExperimentalWidgetButtonsTest extends BrowserTestBase { $this->drupalGet('admin/structure/types/manage/paragraphed_test/form-display'); $option = $this->assertSession()->optionExists('fields[field_paragraphs][type]', 'paragraphs'); $this->assertTrue($option->isSelected()); - // Check that the autocollapse is disabled by default. - $this->assertSession()->pageTextContains('Autocollapse: None'); + // Check that the autocollapse is not displayed if the edit mode is open. + $this->assertSession()->pageTextNotContains('Autocollapse: None'); + $this->assertSession()->pageTextContains('Edit mode: Open'); // Create a new node with 2 paragraphs. $this->drupalGet('node/add/paragraphed_test'); @@ -100,19 +101,19 @@ class ParagraphsExperimentalWidgetButtonsTest extends BrowserTestBase { $this->checkParagraphInMode('field_paragraphs_0', 'edit'); $this->checkParagraphInMode('field_paragraphs_1', 'edit'); - // "Collapse all" enables autocollapse. + // "Collapse all" affects all paragraphs. $this->getSession()->getPage()->findButton('field_paragraphs_collapse_all')->press(); $this->checkParagraphInMode('field_paragraphs_0', 'closed'); $this->checkParagraphInMode('field_paragraphs_1', 'closed'); - // Open the first paragraph and then the second. Opening the second closes - // the first. + // Open the first paragraph and then the second. Opening the second doesn + // not close the first. $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press(); $this->checkParagraphInMode('field_paragraphs_0', 'edit'); $this->checkParagraphInMode('field_paragraphs_1', 'closed'); $this->getSession()->getPage()->findButton('field_paragraphs_1_edit')->press(); - $this->checkParagraphInMode('field_paragraphs_0', 'closed'); + $this->checkParagraphInMode('field_paragraphs_0', 'edit'); $this->checkParagraphInMode('field_paragraphs_1', 'edit'); // "Edit all" disables autocollapse. @@ -120,7 +121,7 @@ class ParagraphsExperimentalWidgetButtonsTest extends BrowserTestBase { $this->checkParagraphInMode('field_paragraphs_0', 'edit'); $this->checkParagraphInMode('field_paragraphs_1', 'edit'); - // Closing and opening a paragraphs does not affect the other one anymore. + // Closing and opening a paragraphs does not affect the other one. $this->getSession()->getPage()->findButton('field_paragraphs_0_collapse')->press(); $this->checkParagraphInMode('field_paragraphs_0', 'closed'); $this->checkParagraphInMode('field_paragraphs_1', 'edit');