diff -u b/config/schema/paragraphs_type.schema.yml b/config/schema/paragraphs_type.schema.yml --- b/config/schema/paragraphs_type.schema.yml +++ b/config/schema/paragraphs_type.schema.yml @@ -97,2 +97,5 @@ - show_duplicate: - type: boolean + features: + label: Available grid layouts + type: sequence + sequence: + type: string diff -u b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php --- b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -128,7 +128,7 @@ 'add_mode' => 'dropdown', 'form_display_mode' => 'default', 'default_paragraph_type' => '', - 'show_duplicate' => TRUE, + 'features' => [], ); } @@ -218,11 +218,13 @@ '#description' => $this->t('When creating a new host entity, a paragraph of this type is added.'), ]; - $elements['show_duplicate'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Show duplicate action'), - '#default_value' => $this->getSetting('show_duplicate'), - '#description' => $this->t('When creating a paragraph, whether to display the duplicate action or not.'), + $elements['features'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Show widget features'), + '#options' => $this->getSettingOptions('features'), + '#default_value' => $this->getSetting('features'), + '#description' => $this->t('When creating a paragraph, whether to display widget actions or not. All features will be enabled if nothing is selected.'), + '#multiple' => TRUE, ]; return $elements; @@ -273,6 +275,12 @@ 'modal' => $this->t('Modal form'), ]; break; + case 'features': + $options = [ + 'duplicate' => $this->t('Duplicate'), + 'multiple_actions' => $this->t('Collapse / Edit all'), + ]; + break; } return isset($options) ? $options : NULL; @@ -291,7 +299,6 @@ $edit_mode = $this->getSettingOptions('edit_mode')[$this->getSetting('edit_mode')]; $closed_mode = $this->getSettingOptions('closed_mode')[$this->getSetting('closed_mode')]; $add_mode = $this->getSettingOptions('add_mode')[$this->getSetting('add_mode')]; - $show_duplicate = $this->getSetting('show_duplicate'); $summary[] = $this->t('Edit mode: @edit_mode', ['@edit_mode' => $edit_mode]); $summary[] = $this->t('Closed mode: @closed_mode', ['@closed_mode' => $closed_mode]); @@ -309,8 +316,17 @@ '@default_paragraph_type' => $this->getDefaultParagraphTypeLabelName() ]); } - $show_duplicate = $show_duplicate ? $this->t('Enabled') : $this->t('Disabled'); - $summary[] = $this->t('Show duplicate: @show_duplicate', ['@show_duplicate' => $show_duplicate]); + $features = $this->getSetting('features'); + $features_labels = []; + foreach ($features as $id => $feature) { + if ($feature) { + $features_labels[] = $this->getSettingOptions('features')[$id]; + } + } + if (empty($features_labels)) { + $features_labels = $this->getSettingOptions('features'); + } + $summary[] = $this->t('Features: @features', ['@features' => implode($features_labels, ', ')]); return $summary; } @@ -501,7 +517,7 @@ 'dropdown_actions' => [], ]; - if ($this->getSetting('show_duplicate')) { + if ($this->isFeatureEnabled('duplicate')) { $widget_actions['dropdown_actions']['duplicate_button'] = [ '#type' => 'submit', '#value' => $this->t('Duplicate'), @@ -2351,7 +2367,7 @@ } // Collapse & expand all. - if ($this->fieldDefinition->getType() == 'entity_reference_revisions' && $this->realItemCount > 1 && empty($field_state['dragdrop'])) { + if ($this->fieldDefinition->getType() == 'entity_reference_revisions' && $this->realItemCount > 1 && empty($field_state['dragdrop']) && $this->isFeatureEnabled('multiple_actions')) { $collapse_all = $this->expandButton([ '#type' => 'submit', '#value' => $this->t('Collapse all'), @@ -2543,2 +2559,19 @@ + /** + * Checks if a widget feature is enabled or not. + * + * @param string $feature + * Feature name to check. + * + * @return bool + * TRUE if the feature is enabled, otherwise FALSE. + */ + protected function isFeatureEnabled($feature) { + $features = $this->getSetting('features'); + if (empty($features) || in_array($feature, $features)) { + return TRUE; + } + return FALSE; + } + } diff -u b/src/Tests/Experimental/ParagraphsExperimentalDuplicateFeatureTest.php b/src/Tests/Experimental/ParagraphsExperimentalDuplicateFeatureTest.php --- b/src/Tests/Experimental/ParagraphsExperimentalDuplicateFeatureTest.php +++ b/src/Tests/Experimental/ParagraphsExperimentalDuplicateFeatureTest.php @@ -91,12 +91,34 @@ // Disable show duplicate action. $this->drupalGet('admin/structure/types/manage/paragraphed_test/form-display'); + $this->assertText('Features: Duplicate, Collapse / Edit all'); $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_settings_edit'); - $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][show_duplicate]' => FALSE], t('Update')); + $edit = [ + 'fields[field_paragraphs][settings_edit_form][settings][features][duplicate]' => FALSE, + 'fields[field_paragraphs][settings_edit_form][settings][features][multiple_actions]' => TRUE + ]; + $this->drupalPostForm(NULL, $edit, t('Update')); + $this->assertText('Features: Collapse / Edit all'); $this->drupalPostForm(NULL, [], 'Save'); $this->drupalGet('node/' . $node->id() . '/edit'); // Check that the duplicate action is not present. $this->assertNoField('field_paragraphs_0_duplicate'); + $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A'); + + // Enable show duplicate action. + $this->drupalGet('admin/structure/types/manage/paragraphed_test/form-display'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_settings_edit'); + $edit = [ + 'fields[field_paragraphs][settings_edit_form][settings][features][duplicate]' => TRUE, + 'fields[field_paragraphs][settings_edit_form][settings][features][multiple_actions]' => FALSE + ]; + $this->drupalPostForm(NULL, $edit, t('Update')); + $this->assertText('Features: Duplicate'); + $this->drupalPostForm(NULL, [], 'Save'); + $this->drupalGet('node/' . $node->id() . '/edit'); + // Check that the duplicate action is present. + $this->assertField('field_paragraphs_0_duplicate'); + $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'A'); } /** only in patch2: unchanged: --- a/src/Tests/Experimental/ParagraphsExperimentalHeaderActionsTest.php +++ b/src/Tests/Experimental/ParagraphsExperimentalHeaderActionsTest.php @@ -112,6 +112,9 @@ class ParagraphsExperimentalHeaderActionsTest extends ParagraphsExperimentalTest $this->assertText('First text'); $this->assertText('Second text'); $this->assertNoText('Third text'); + $this->assertField('field_paragraphs_collapse_all'); + $this->assertField('field_paragraphs_edit_all'); + $this->drupalPostForm(NULL, [], t('Save')); // Check that the drag and drop button is present when there is a paragraph // and that it is not shown when the paragraph is deleted. @@ -119,6 +122,28 @@ class ParagraphsExperimentalHeaderActionsTest extends ParagraphsExperimentalTest $this->assertRaw('name="field_paragraphs_dragdrop_mode"'); $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove'); $this->assertNoRaw('name="field_paragraphs_dragdrop_mode"'); + + // Disable show multiple actions. + $this->drupalGet('admin/structure/types/manage/paragraphed_test/form-display'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_settings_edit'); + $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][features][multiple_actions]' => FALSE], t('Update')); + $this->drupalPostForm(NULL, [], 'Save'); + $this->drupalGet('node/' . $node->id() . '/edit'); + // Check that the collapse/edit all actions are not present. + $this->assertNoField('field_paragraphs_collapse_all'); + $this->assertNoField('field_paragraphs_edit_all'); + $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'First text'); + + // Enable show multiple actions. + $this->drupalGet('admin/structure/types/manage/paragraphed_test/form-display'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_settings_edit'); + $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][features][multiple_actions]' => TRUE], t('Update')); + $this->drupalPostForm(NULL, [], 'Save'); + $this->drupalGet('node/' . $node->id() . '/edit'); + // Check that the collapse/edit all actions are present. + $this->assertField('field_paragraphs_collapse_all'); + $this->assertField('field_paragraphs_edit_all'); + $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'First text'); } /**