diff -u b/config/schema/paragraphs_collection.schema.yml b/config/schema/paragraphs_collection.schema.yml --- b/config/schema/paragraphs_collection.schema.yml +++ b/config/schema/paragraphs_collection.schema.yml @@ -11,9 +11,6 @@ default: type: string label: 'Default group style' - default: - type: string - label: Default style paragraphs.behavior.settings.grid_layout: type: paragraphs.behavior.settings_base diff -u b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php --- b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php +++ b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php @@ -161,7 +161,8 @@ ]; // @todo: Remove getCompleteFormState() after https://www.drupal.org/project/drupal/issues/2798261. $group_key = ['behavior_plugins', $this->getPluginId(), 'settings', 'groups']; - $groups = $form_state->getCompleteFormState()->getValue($group_key, $this->configuration['groups']); + $groups = $form_state->getCompleteFormState() + ->getValue($group_key, $this->configuration['groups']); $form['groups_defaults'] = [ '#type' => 'container', '#prefix' => '
', @@ -250,18 +251,25 @@ } } - /** - * {@inheritdoc} - */ - public function settingsSummary(Paragraph $paragraph) { - $styles = $this->yamlStyleDiscovery->getStyleOptions(); - if ($style = $paragraph->getBehaviorSetting($this->getPluginId(), 'style')) { - if ($style != $this->configuration['default']) { - return [$this->t('Style: @style', ['@style' => $styles[$style]])]; - } - } - return []; - } + /** + * {@inheritdoc} + */ + public function settingsSummary(Paragraph $paragraph) { + $style_options = $this->yamlStyleDiscovery->getStyleOptions(); + $summary = []; + if ($styles = $paragraph->getBehaviorSetting($this->getPluginId(), 'styles')) { + foreach ($styles as $group_id => $style) { + if (!isset($this->configuration['groups'][$group_id]) || $style != $this->configuration['groups'][$group_id]['default']) { + $summary[] = $this->t('@group: @style', [ + '@group' => $this->yamlStyleGroupDiscovery->getGroupLabel($group_id), + '@style' => $style_options[$style] + ]); + } + } + return [implode($summary, ', ')]; + } + return []; + } /** * Ajax callback for loading the style description for the currently @@ -304,7 +312,8 @@ if ($paragraph->getParagraphType()->hasEnabledBehaviorPlugin('style')) { $defaults = $paragraph->getParagraphType()->getBehaviorPlugin('style')->getConfiguration()['groups']; if ($paragraph_style = $paragraph->getBehaviorSetting('style', ['styles', key($defaults)], reset($defaults)['default'])) { - $style = \Drupal::service('paragraphs_collection.style_discovery')->getStyle($paragraph_style); + $style = \Drupal::service('paragraphs_collection.style_discovery') + ->getStyle($paragraph_style); if (!empty($style['template'])) { return $style['template']; } diff -u b/src/StyleGroupDiscovery.php b/src/StyleGroupDiscovery.php --- b/src/StyleGroupDiscovery.php +++ b/src/StyleGroupDiscovery.php @@ -117,6 +117,13 @@ } /** + * {@inheritdoc} + */ + public function getGroupId($group_label) { + return strtolower(str_replace(' ', '_', $group_label)); + } + + /** * Gets the YAML discovery. * * @return \Drupal\Core\Discovery\YamlDiscovery diff -u b/src/StyleGroupDiscoveryInterface.php b/src/StyleGroupDiscoveryInterface.php --- b/src/StyleGroupDiscoveryInterface.php +++ b/src/StyleGroupDiscoveryInterface.php @@ -30,2 +30,13 @@ + /** + * Gets group label. + * + * @param string $group_label + * The group label. + * + * @return string + * The group id. + */ + public function getGroupId($group_label); + } diff -u b/src/Tests/ParagraphsStylePluginTest.php b/src/Tests/ParagraphsStylePluginTest.php --- b/src/Tests/ParagraphsStylePluginTest.php +++ b/src/Tests/ParagraphsStylePluginTest.php @@ -57,7 +57,11 @@ $this->assertText('Style groups field is required.'); $edit = [ - 'behavior_plugins[style][enabled]' => TRUE, 'behavior_plugins[style][settings][groups][]' => ['regular_test_group'], ]; + $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['regular_test_group'], + 'behavior_plugins[style][settings][groups_defaults][regular_test_group][default]' => '', + ]; $this->drupalPostForm(NULL, $edit, t('Save')); @@ -67,7 +71,7 @@ // Check that we have style plugin. $this->assertText('Style'); - $this->assertField('paragraphs[0][behavior_plugins][style][style_wrapper][style]'); + $this->assertField('paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]'); // Check that the style options are sorted alphabetically. $styles = $this->xpath('//select[contains(@id, :id)]', [':id' => 'edit-paragraphs-0-behavior-plugins-style-style']); @@ -82,9 +86,16 @@ 'behavior_plugins[style][enabled]' => TRUE, 'behavior_plugins[style][settings][groups][]' => ['italic_test_group'], ]; + $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['italic_test_group'], + 'behavior_plugins[style][settings][groups_defaults][italic_test_group][default]' => '', + ]; $this->drupalPostForm(NULL, $edit, t('Save')); // Check that the style without a style group is no longer available. $this->drupalGet('node/add/paragraphed_test'); + $this->drupalPostAjaxForm(NULL, [], 'paragraphs_test_style_plugin_add_more'); + // Since Italic Group defines only two styles, assert that only they appear. $styles = $this->xpath('//select[contains(@id, :id)]', [':id' => 'edit-paragraphs-0-behavior-plugins-style-style']); $this->assertEqual(3, count($styles[0]->option)); @@ -94,14 +105,14 @@ // Configure Regular as a default style. $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); - $this->assertFieldByName('behavior_plugins[style][settings][defaults][italic_test_group]'); + $this->assertFieldByName('behavior_plugins[style][settings][groups_defaults][italic_test_group][default]', ''); $edit = [ 'behavior_plugins[style][settings][groups][]' => ['regular_test_group'], ]; $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); $edit = [ 'behavior_plugins[style][settings][groups][]' => ['regular_test_group'], - 'behavior_plugins[style][settings][defaults][regular_test_group]' => 'regular', + 'behavior_plugins[style][settings][groups_defaults][regular_test_group][default]' => 'regular', ]; $this->drupalPostForm(NULL, $edit, t('Save')); @@ -129,7 +140,7 @@ // Assert default value for the style selection. $node = $this->getNodeByTitle('style_plugin_node'); $this->drupalGet('node/' . $node->id() . '/edit'); - $this->assertFieldByName('paragraphs[0][behavior_plugins][style][style_wrapper][regular_test_group]', 'regular'); + $this->assertFieldByName('paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]', 'regular'); } /** @@ -177,22 +188,26 @@ $this->assertEqual([], $style_plugin->settingsSummary($paragraph)); // Use bold style for this container. - $paragraph->setBehaviorSettings('style', ['style' => 'bold']); + $paragraph->setBehaviorSettings('style', ['styles' => ['bold_test_group' => 'bold']]); $paragraph->save(); - $this->assertEqual(['Style: Bold'], $style_plugin->settingsSummary($paragraph)); + $this->assertEqual(['Bold Test Group: Bold'], $style_plugin->settingsSummary($paragraph)); // Check the settings summary in a closed mode. $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed'); $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertRaw('edit-field-paragraphs-0-top-icons'); - $this->assertRaw('Test text 1., Style: Bold'); + $this->assertRaw('Test text 1., Bold Test Group: Bold'); // Configure style bold as default. $edit = [ 'behavior_plugins[style][settings][groups][]' => ['bold_test_group'], + ]; + $this->drupalPostAjaxForm('admin/structure/paragraphs_type/' . $paragraph->getType(), $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['bold_test_group'], 'behavior_plugins[style][settings][groups_defaults][bold_test_group][default]' => 'bold', ]; - $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph->getType(), $edit, t('Save')); + $this->drupalPostForm(NULL, $edit, t('Save')); // Check that the settings summary does not show the default style. $this->drupalGet('node/' . $node->id() . '/edit'); only in patch2: unchanged: --- a/modules/paragraphs_collection_demo/paragraphs_collection_demo.install +++ b/modules/paragraphs_collection_demo/paragraphs_collection_demo.install @@ -82,7 +82,7 @@ function _paragraphs_collection_demo_add_background_plugin_field() { ], 'style' => [ 'enabled' => TRUE, - 'group' => '', + 'groups' => ['general_group'], ], ]); $container->save(); only in patch2: unchanged: --- a/paragraphs_collection.install +++ b/paragraphs_collection.install @@ -4,6 +4,8 @@ * Installation hooks for paragraphs collection module. */ +use Drupal\paragraphs\Entity\ParagraphsType; + /** * Add default config for enabled styles. */ @@ -13,3 +15,27 @@ function paragraphs_collection_update_8001() { ->set('enabled_styles', []) ->save(); } + +/** + * Update style plugin config of Paragraph types. + */ +function paragraphs_collection_update_8002() { + $paragraph_types = ParagraphsType::loadMultiple(); + foreach ($paragraph_types as $id => $paragraph_type) { + /** @var ParagraphsType $paragraph_type */ + if ($paragraph_type->hasEnabledBehaviorPlugin('style')) { + $default = ''; + $group = $paragraph_type->getBehaviorPlugin('style')->getConfiguration()['group']; + $default_style = $paragraph_type->getBehaviorPlugin('style')->getConfiguration()['default']; + $group_id = \Drupal::service('paragraphs_collection.style_group_discovery')->getGroupId($group); + if ($style = \Drupal::service('paragraphs_collection.style_discovery')->getStyle($default_style)) { + if (in_array($group_id, $style['groups'])) { + $default = $default_style; + } + } + $config = ['groups' => [$group_id => ['default' => $default]]]; + $paragraph_type->getBehaviorPlugin('style')->setConfiguration($config); + $paragraph_type->save(); + } + } +} only in patch2: unchanged: --- /dev/null +++ b/style_group_translation_patterns.yml @@ -0,0 +1,4 @@ +translation_patterns: + - matches: '*.style_group.yml' + translatable_keys: + - label