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 @@ -261,7 +261,7 @@ // In case the current style is not set, fallback to the default style. // If default style is set to none, no style will be applied. $paragraph_styles = []; - if ($this->configuration['defaults']) { + if (isset($this->configuration['defaults'])) { foreach ($this->configuration['defaults'] as $key => $value) { $paragraph_styles[$key] = $paragraph->getBehaviorSetting($this->getPluginId(), $key, $this->configuration['defaults'][$key]); } diff -u b/src/Tests/ParagraphsStylePluginTest.php b/src/Tests/ParagraphsStylePluginTest.php --- b/src/Tests/ParagraphsStylePluginTest.php +++ b/src/Tests/ParagraphsStylePluginTest.php @@ -323,2 +323,126 @@ + /** + * Tests the style selection plugin settings and functionality. + */ + public function testMultipleGroups() { + // Install Paragraph Collection Test in order to have styles. + \Drupal::service('module_installer')->install(['paragraphs_collection_test']); + + $this->addParagraphedContentType('paragraphed_test', 'paragraphs'); + $this->loginAsAdmin([ + 'edit any paragraphed_test content', + 'edit behavior plugin settings', + ]); + $this->drupalGet('admin/structure/paragraphs_type/add'); + + // Create Paragraph type with Style plugin enabled. + $paragraph_type = 'test_style_plugin'; + $this->addParagraphsType($paragraph_type); + // Add a text field. + $this->fieldUIAddExistingField('admin/structure/paragraphs_type/' . $paragraph_type, 'paragraphs_text', $paragraph_type); + $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); + $this->assertFieldByName('behavior_plugins[style][settings][groups][]'); + $this->assertFieldByName('behavior_plugins[style][settings][default]', ''); + $edit = [ + 'behavior_plugins[style][enabled]' => TRUE, + 'behavior_plugins[style][settings][groups][]' => [], + 'behavior_plugins[style][settings][default]' => '', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + // Create paragraphed content. + $this->drupalGet('node/add/paragraphed_test'); + $this->drupalPostAjaxForm(NULL, [], 'paragraphs_test_style_plugin_add_more'); + + // Check that a style without a style group is available. + $options = $this->xpath('//select[@name=:name]//option[normalize-space(text())=:text]', [ + ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][style]', + ':text' => 'Groupless', + ]); + $this->assertTrue(isset($options[0]), 'Groupless style available.'); + + // Check that the style options are sorted alphabetically. + $styles = $this->xpath('//select[contains(@id, :id)]', [':id' => 'edit-paragraphs-0-behavior-plugins-style-style']); + $this->assertEqual('- Default -', $styles[0]->option[0]); + $this->assertEqual('Bold', $styles[0]->option[1]); + $this->assertEqual('Groupless', $styles[0]->option[2]); + $this->assertEqual('Italic', $styles[0]->option[3]); + $this->assertEqual('Regular', $styles[0]->option[4]); + $this->assertEqual('Underline', $styles[0]->option[5]); + + // Restrict the paragraphs type to the "Italic Test Group" style group. + $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['Italic Test Group'], + ]; + $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][enabled]' => TRUE, + 'behavior_plugins[style][settings][groups][]' => ['Italic Test Group'], + 'behavior_plugins[style][settings][defaults][italic_test_group]' => 'italic', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); + + // 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(2, count($styles[0]->option)); + $this->assertEqual('- Italic -', $styles[0]->option[0]); + $this->assertEqual('Bold', $styles[0]->option[1]); + $edit = [ + 'title[0][value]' => 'title_to_remember', + 'paragraphs[0][subform][paragraphs_text][0][value]' => 'text to apply styles' + ]; + $this->drupalPostForm(NULL, $edit, 'Save'); + $this->assertRaw('paragraphs-behavior-style--italic'); + + // Configure Regular as a default style. + $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['Italic Test Group', 'Regular Test Group'], + ]; + $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['Italic Test Group', 'Regular Test Group'], + 'behavior_plugins[style][settings][defaults][regular_test_group]' => 'regular', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + $node = $this->getNodeByTitle('title_to_remember'); + $this->drupalGet('node/' . $node->id() . '/edit'); + // Since Italic Group defines only two styles, assert that only they appear. + $styles = $this->xpath('//select[contains(@name, :name)]', [':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][regular_test_group]']); + $this->assertEqual(2, count($styles[0]->option)); + $this->assertEqual('- Regular -', $styles[0]->option[0]); + $this->assertEqual('Bold', $styles[0]->option[1]); + $styles = $this->xpath('//select[contains(@name, :name)]', [':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][italic_test_group]']); + $this->assertEqual(2, count($styles[0]->option)); + $this->assertEqual('- Italic -', $styles[0]->option[0]); + $this->assertEqual('Bold', $styles[0]->option[1]); + $edit = [ + 'paragraphs[0][behavior_plugins][style][style_wrapper][italic_test_group]' => 'bold', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + $this->drupalGet('node/' . $node->id()); + $this->assertRaw('paragraphs-behavior-style--regular'); + $this->assertRaw('paragraphs-behavior-style--bold'); + + // Configure Regular as a default style. + $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); + $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]' => 'bold', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + } + }