diff --git a/config/schema/paragraphs_collection.schema.yml b/config/schema/paragraphs_collection.schema.yml
index 44aa228..fbbc01f 100644
--- a/config/schema/paragraphs_collection.schema.yml
+++ b/config/schema/paragraphs_collection.schema.yml
@@ -4,9 +4,21 @@ paragraphs.behavior.settings.style:
group:
type: string
label: Style group
+ groups:
+ type: sequence
+ label: 'Messages'
+ sequence:
+ type: string
+ label: 'Message'
default:
type: string
label: Default style
+ defaults:
+ type: sequence
+ label: 'Test'
+ sequence:
+ type: string
+ label: 'Test'
paragraphs.behavior.settings.grid_layout:
type: paragraphs.behavior.settings_base
diff --git a/modules/paragraphs_collection_demo/src/Tests/ParagraphsCollectionDemoTest.php b/modules/paragraphs_collection_demo/src/Tests/ParagraphsCollectionDemoTest.php
index 2899252..bead67c 100644
--- a/modules/paragraphs_collection_demo/src/Tests/ParagraphsCollectionDemoTest.php
+++ b/modules/paragraphs_collection_demo/src/Tests/ParagraphsCollectionDemoTest.php
@@ -36,7 +36,10 @@ class ParagraphsCollectionDemoTest extends ParagraphsExperimentalTestBase {
$this->drupalGet('admin/structure/paragraphs_type/container');
$this->assertText('Container');
$this->assertFieldChecked('edit-behavior-plugins-style-enabled');
- $this->assertFieldById('edit-behavior-plugins-style-settings-group', '');
+ $this->assertNoOptionSelected('edit-behavior-plugins-style-settings-groups', 'General Group');
+ $this->assertNoOptionSelected('edit-behavior-plugins-style-settings-groups', 'Slideshow Group');
+ $options = $this->xpath('//*[contains(@id,"edit-behavior-plugins-style-settings-groups")]/option');
+ $this->assertEqual(2, count($options));
// @todo When other plugins are available, add assertion.
$this->drupalGet('admin/structure/paragraphs_type/text');
diff --git a/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php
index f111814..7d72959 100644
--- a/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php
+++ b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php
@@ -81,16 +81,39 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
'id' => $wrapper_id,
],
];
- $form['style_wrapper']['style'] = [
- '#type' => 'select',
- '#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 -');
+
+ 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',
+ '#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 (!isset($this->configuration['defaults'][$group_id])) {
+ $form['style_wrapper'][$group_id]['#empty_option'] = $this->t('- Default -');
+ }
+ }
+ }
+ 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']),
+ '#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 -');
+ }
}
return $form;
@@ -129,31 +152,57 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
- $form['group'] = [
+ $form['groups'] = [
'#type' => 'select',
+ '#title' => $this->t('Style groups'),
+ '#multiple' => TRUE,
'#empty_option' => $this->t('- None -'),
'#options' => $this->yamlStyleDiscovery->getStyleGroups(),
- '#title' => $this->t('Style group'),
'#description' => $this->t('Restrict available styles to a certain style group. Select "- None -" to allow all styles.'),
- '#default_value' => $this->configuration['group'],
+ '#default_value' => $this->configuration['groups'],
'#ajax' => [
'callback' => [$this, 'updateDefaultStyle'],
'wrapper' => 'style-wrapper',
],
];
// @todo: Remove getCompleteFormState() after https://www.drupal.org/project/drupal/issues/2798261.
- $group_key = ['behavior_plugins', $this->getPluginId(), 'settings', 'group'];
- $group = $form_state->getCompleteFormState()->getValue($group_key, $this->configuration['group']);
- $form['default'] = [
- '#type' => 'select',
- '#empty_option' => $this->t('- None -'),
- '#options' => $this->yamlStyleDiscovery->getStyleOptions($group),
- '#title' => $this->t('Default style'),
- '#description' => $this->t('This style will be default option on a behavior form.'),
- '#default_value' => $this->configuration['default'],
- '#prefix' => '
',
- '#suffix' => '
',
- ];
+ $group_key = ['behavior_plugins', $this->getPluginId(), 'settings', 'groups'];
+ $groups = $form_state->getCompleteFormState()->getValue($group_key, $this->configuration['groups']);
+ if (count($groups) != 0) {
+ $form['defaults'] = [
+ '#type' => 'container',
+ '#prefix' => '',
+ '#suffix' => '
',
+ ];
+ foreach($groups as $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' => $default,
+ ];
+ }
+ }
+ else {
+ $form['default'] = [
+ '#type' => 'select',
+ '#empty_option' => $this->t('- None -'),
+ '#options' => $this->yamlStyleDiscovery->getStyleOptions(reset($groups)),
+ '#title' => $this->t('Default style'),
+ '#description' => $this->t('This style will be default option on a behavior form.'),
+ '#default_value' => $this->configuration['default'],
+ '#prefix' => '',
+ '#suffix' => '
',
+ ];
+ }
+
return $form;
}
@@ -162,9 +211,15 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
*/
public static function updateDefaultStyle(array $form, FormStateInterface $form_state) {
$group_select = $form_state->getTriggeringElement();
+ $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));
- return $settings_form['default'];
+ if (count($groups) != 0) {
+ return $settings_form['defaults'];
+ }
+ else {
+ return $settings_form['default'];
+ }
}
/**
@@ -182,7 +237,9 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
*/
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') ?: [];
}
/**
@@ -191,7 +248,9 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
public function defaultConfiguration() {
return [
'group' => '',
+ 'groups' => [],
'default' => '',
+ 'defaults' => [],
];
}
@@ -201,20 +260,44 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
// 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_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'])) {
- $build['#attached']['library'] = [];
+ $paragraph_styles = [];
+ if ($this->configuration['defaults']) {
+ foreach ($this->configuration['defaults'] as $key => $value) {
+ $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']);
+ }
+ }
}
- $build['#attached']['library'] = array_merge($style['libraries'], $build['#attached']['library']);
+ }
+ else {
+ $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'])) {
+ $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']);
+ // Add CSS classes from style configuration if they are defined.
+ if (!empty($style['classes'])) {
+ $build['#attributes']['class'] = array_merge($style['classes'], $build['#attributes']['class']);
+ }
}
}
+
}
/**
@@ -269,14 +352,25 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF
*/
public static function getStyleTemplate(ParagraphInterface $paragraph) {
if ($paragraph->getParagraphType()->hasEnabledBehaviorPlugin('style')) {
- $default = $paragraph->getParagraphType()->getBehaviorPlugin('style')->getConfiguration()['default'];
- if ($paragraph_style = $paragraph->getBehaviorSetting('style', 'style', $default)) {
- $style = \Drupal::service('paragraphs_collection.style_discovery')
- ->getStyle($paragraph_style);
- if (!empty($style['template'])) {
- return $style['template'];
- }
- };
+ if ($defaults = $paragraph->getParagraphType()->getBehaviorPlugin('style')->getConfiguration()['defaults']) {
+ if ($paragraph_style = $paragraph->getBehaviorSetting('style', 'style', reset($defaults))) {
+ $style = \Drupal::service('paragraphs_collection.style_discovery')
+ ->getStyle($paragraph_style);
+ if (!empty($style['template'])) {
+ return $style['template'];
+ }
+ };
+ }
+ else {
+ $default = $paragraph->getParagraphType()->getBehaviorPlugin('style')->getConfiguration()['default'];
+ if ($paragraph_style = $paragraph->getBehaviorSetting('style', 'style', $default)) {
+ $style = \Drupal::service('paragraphs_collection.style_discovery')
+ ->getStyle($paragraph_style);
+ if (!empty($style['template'])) {
+ return $style['template'];
+ }
+ };
+ }
}
}
diff --git a/src/Tests/ParagraphsStylePluginTest.php b/src/Tests/ParagraphsStylePluginTest.php
index 161b348..fed14f8 100644
--- a/src/Tests/ParagraphsStylePluginTest.php
+++ b/src/Tests/ParagraphsStylePluginTest.php
@@ -48,11 +48,11 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase {
// 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][group]', '');
+ $this->assertFieldByName('behavior_plugins[style][settings][groups][]');
$this->assertFieldByName('behavior_plugins[style][settings][default]', '');
$edit = [
'behavior_plugins[style][enabled]' => TRUE,
- 'behavior_plugins[style][settings][group]' => '',
+ 'behavior_plugins[style][settings][groups][]' => [],
'behavior_plugins[style][settings][default]' => '',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
@@ -83,20 +83,22 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase {
// Restrict the paragraphs type to the "Italic Test Group" style group.
$this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
- $this->assertFieldByName('behavior_plugins[style][settings][group]', '');
+ $this->assertFieldByName('behavior_plugins[style][settings][groups][]');
$edit = [
'behavior_plugins[style][enabled]' => TRUE,
- 'behavior_plugins[style][settings][group]' => '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 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase {
// 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][group]' => 'Regular Test Group',
+ 'behavior_plugins[style][settings][groups][]' => ['Regular Test Group'],
];
- $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][group]');
+ $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]');
$edit = [
- 'behavior_plugins[style][settings][group]' => 'Regular Test Group',
- 'behavior_plugins[style][settings][default]' => 'regular',
+ 'behavior_plugins[style][settings][groups][]' => 'Regular Test Group',
+ 'behavior_plugins[style][settings][defaults][regular_test_group]' => 'regular',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
@@ -145,7 +147,7 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase {
// 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');
}
/**
@@ -205,7 +207,7 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase {
// Configure style bold as default.
$edit = [
- 'behavior_plugins[style][settings][group]' => '',
+ 'behavior_plugins[style][settings][groups][]' => [],
'behavior_plugins[style][settings][default]' => 'bold',
];
$this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph->getType(), $edit, t('Save'));
@@ -234,7 +236,8 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase {
// Add a text field.
$this->fieldUIAddExistingField('admin/structure/paragraphs_type/' . $paragraph_type, 'paragraphs_text');
$this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
- $this->assertFieldByName('behavior_plugins[style][settings][group]', '');
+ $options = $this->xpath('//*[contains(@id,"edit-behavior-plugins-style-settings-groups")]/option');
+ $this->assertEqual(0, count($options));
$edit = [
'behavior_plugins[style][enabled]' => TRUE,
];
@@ -261,7 +264,7 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase {
// Add a text field.
$this->fieldUIAddExistingField('admin/structure/paragraphs_type/' . $paragraph_type, 'paragraphs_text');
$this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
- $this->assertFieldByName('behavior_plugins[style][settings][group]', '');
+ $this->assertFieldByName('behavior_plugins[style][settings][groups][]');
$edit = [
'behavior_plugins[style][enabled]' => TRUE,
];
@@ -308,11 +311,11 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase {
// Check that styles are not shown in style config when disabled.
$edit = [
- 'behavior_plugins[style][settings][group]' => 'Italic Test Group'
+ 'behavior_plugins[style][settings][groups][]' => 'Italic Test Group'
];
$this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, 'Save');
$this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
- $options = $this->xpath('//*[contains(@name,"behavior_plugins[style][settings][default]")]/option');
+ $options = $this->xpath('//*[contains(@name,"behavior_plugins[style][settings][defaults][italic_test_group]")]/option');
$this->assertEqual(2, count($options));
$this->assertEqual($options[0], '- None -');
$this->assertEqual($options[1], 'Italic');