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 @@ -81,18 +81,23 @@ 'id' => $wrapper_id, ], ]; - if (count($this->configuration['groups']) > 1) { + + if (count($this->configuration['groups']) != 0) { foreach ($this->configuration['groups'] as $group) { $group_id = strtolower(str_replace(' ', '_', $group)); + $plugin_default = ''; + if (isset($this->configuration['defaults'][$group_id])) { + $plugin_default = $this->configuration['defaults'][$group_id]; + } $form['style_wrapper'][$group_id] = [ '#type' => 'select', - '#options' => $this->getStyleOptions($group, $this->configuration['defaults'][$group_id]), - '#default_value' => $paragraph->getBehaviorSetting($this->getPluginId(), $group_id, $this->configuration['default']), '#title' => !empty($this->configuration['groups'][$group]) ? t('%group Style', ['%group' => $this->configuration['groups'][$group]]) : t('Style'), + '#options' => $this->getStyleOptions($group, $plugin_default), + '#default_value' => $paragraph->getBehaviorSetting($this->getPluginId(), $group_id, $this->configuration['default']), '#attributes' => ['class' => ['paragraphs-style']], ]; // Allow empty option in case there is no default style configured. - if (!$this->configuration['defaults'][$group_id]) { + if (!isset($this->configuration['defaults'][$group_id])) { $form['style_wrapper'][$group_id]['#empty_option'] = $this->t('- Default -'); } } @@ -100,15 +105,15 @@ else { $form['style_wrapper']['style'] = [ '#type' => 'select', + '#title' => !empty($this->configuration['group']) ? t('%group Style', ['%group' => $this->configuration['group']]) : t('Style'), '#options' => $this->getStyleOptions($this->configuration['group'], $this->configuration['default']), '#default_value' => $paragraph->getBehaviorSetting($this->getPluginId(), 'style', $this->configuration['default']), - '#title' => !empty($this->configuration['group']) ? t('%group Style', ['%group' => $this->configuration['group']]) : t('Style'), '#attributes' => ['class' => ['paragraphs-style']], ]; - } - // Allow empty option in case there is no default style configured. - if (!$this->configuration['default']) { - $form['style_wrapper']['style']['#empty_option'] = $this->t('- Default -'); + // Allow empty option in case there is no default style configured. + if (!$this->configuration['default']) { + $form['style_wrapper']['style']['#empty_option'] = $this->t('- Default -'); + } } return $form; @@ -149,35 +154,39 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['groups'] = [ '#type' => 'select', + '#title' => $this->t('Style groups'), '#multiple' => TRUE, '#empty_option' => $this->t('- None -'), '#options' => $this->yamlStyleDiscovery->getStyleGroups(), - '#title' => $this->t('Style groups'), '#description' => $this->t('Restrict available styles to a certain style group. Select "- None -" to allow all styles.'), '#default_value' => $this->configuration['groups'], '#ajax' => [ 'callback' => [$this, 'updateDefaultStyle'], 'wrapper' => 'style-wrapper', ], - '#size' => 4 ]; // @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']); - if (count($groups) > 1) { + if (count($groups) != 0) { $form['defaults'] = [ '#type' => 'container', '#prefix' => '
', '#suffix' => '
', ]; foreach($groups as $group) { - $form['defaults'][strtolower(str_replace(' ', '_', $group))] = [ + $default = ''; + $group_name = strtolower(str_replace(' ', '_', $group)); + if (isset($this->configuration['defaults'][$group_name])) { + $default = $this->configuration['defaults'][$group_name]; + } + $form['defaults'][$group_name] = [ '#type' => 'select', '#title' => $this->t($group . ' default style'), '#empty_option' => $this->t('- None -'), '#options' => $this->yamlStyleDiscovery->getStyleOptions($group), '#description' => $this->t('This style will be default option on a behavior form.'), - '#default_value' => $this->configuration['defaults'][strtolower(str_replace(' ', '_', $group))], + '#default_value' => $default, ]; } } @@ -205,7 +214,7 @@ $groups = $form_state->getValue($group_select['#array_parents']); // Gets the behavior plugin settings form. $settings_form = NestedArray::getValue($form, array_slice($group_select['#array_parents'], 0, -1)); - if (count($groups) > 1) { + if (count($groups) != 0) { return $settings_form['defaults']; } else { @@ -227,9 +236,10 @@ */ */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['group'] = $form_state->getValue('group'); $this->configuration['groups'] = $form_state->getValue('groups'); $this->configuration['default'] = $form_state->getValue('default'); - $this->configuration['defaults'] = $form_state->getValue('defaults'); + $this->configuration['defaults'] = $form_state->getValue('defaults') ?: []; } /** @@ -253,15 +263,27 @@ $paragraph_styles = []; if ($this->configuration['defaults']) { foreach ($this->configuration['defaults'] as $key => $value) { - $paragraph_styles[] = $paragraph->getBehaviorSetting($this->getPluginId(), $key, $this->configuration['defaults']); + $paragraph_styles[$key] = $paragraph->getBehaviorSetting($this->getPluginId(), $key, $this->configuration['defaults'][$key]); + } + foreach ($paragraph_styles as $key => $value) { + $style = $this->yamlStyleDiscovery->getStyle($value, $this->configuration['defaults'][$key]); + if ($style) { + $build['#attributes']['class'][] = 'paragraphs-behavior-' . $this->getPluginId() . '--' . $style['name']; + if (!isset($build['#attached']['library'])) { + $build['#attached']['library'] = []; + } + $build['#attached']['library'] = array_merge($style['libraries'], $build['#attached']['library']); + + // Add CSS classes from style configuration if they are defined. + if (!empty($style['classes'])) { + $build['#attributes']['class'] = array_merge($style['classes'], $build['#attributes']['class']); + } + } } } else { - $paragraph_styles[] = $paragraph->getBehaviorSetting($this->getPluginId(), 'style', $this->configuration['default']); - } - - foreach ($paragraph_styles as $key) { - $style = $this->yamlStyleDiscovery->getStyle($key, $this->configuration['defaults'][$key]); + $paragraph_style = $paragraph->getBehaviorSetting($this->getPluginId(), 'style', $this->configuration['default']); + $style = $this->yamlStyleDiscovery->getStyle($paragraph_style, $this->configuration['default']); if ($style) { $build['#attributes']['class'][] = 'paragraphs-behavior-' . $this->getPluginId() . '--' . $style['name']; if (!isset($build['#attached']['library'])) { diff -u b/src/Tests/ParagraphsStylePluginTest.php b/src/Tests/ParagraphsStylePluginTest.php --- b/src/Tests/ParagraphsStylePluginTest.php +++ b/src/Tests/ParagraphsStylePluginTest.php @@ -86,17 +86,19 @@ $this->assertFieldByName('behavior_plugins[style][settings][groups][]'); $edit = [ 'behavior_plugins[style][enabled]' => TRUE, - 'behavior_plugins[style][settings][groups][]' => 'Italic Test Group', + 'behavior_plugins[style][settings][groups][]' => ['Italic Test Group'], ]; $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'); $this->assertText('Style'); - $this->assertField('paragraphs[0][behavior_plugins][style][style_wrapper][style]'); + $this->assertField('paragraphs[0][behavior_plugins][style][style_wrapper][italic_test_group]'); $options = $this->xpath('//select[@name=:name]//option[normalize-space(text())=:text]', [ - ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][style]', + ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][italic_test_group]', ':text' => 'Groupless', ]); $this->assertTrue(!isset($options[0]), 'Groupless style not available.'); @@ -110,14 +112,14 @@ // Configure Regular as a default style. $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); - $this->assertFieldByName('behavior_plugins[style][settings][default]', ''); + $this->assertFieldByName('behavior_plugins[style][settings][defaults][italic_test_group]'); $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][default]' => 'regular', + 'behavior_plugins[style][settings][defaults][regular_test_group]' => 'regular', ]; $this->drupalPostForm(NULL, $edit, t('Save')); @@ -145,7 +147,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][style]', 'regular'); + $this->assertFieldByName('paragraphs[0][behavior_plugins][style][style_wrapper][regular_test_group]', 'regular'); } /**