diff --git a/config/schema/paragraphs_collection.schema.yml b/config/schema/paragraphs_collection.schema.yml index fa7a055..4a69ad9 100644 --- a/config/schema/paragraphs_collection.schema.yml +++ b/config/schema/paragraphs_collection.schema.yml @@ -2,11 +2,17 @@ paragraphs.behavior.settings.style: type: paragraphs.behavior.settings_base mapping: group: - type: string + type: sequence label: Style group + sequence: + type: string default: type: string label: Default style + deafults: + type: sequence + sequence: + type: string paragraphs.behavior.settings.grid_layout: type: paragraphs.behavior.settings_base mapping: diff --git a/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php index 75e08a6..eec0744 100644 --- a/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php +++ b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php @@ -130,6 +130,7 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['group'] = [ '#type' => 'select', + '#multiple' => TRUE, '#empty_option' => $this->t('- None -'), '#options' => $this->yamlStyleDiscovery->getStyleGroups(), '#title' => $this->t('Style group'), @@ -142,17 +143,37 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF ]; // @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' => '
', - ]; + $groups = $form_state->getCompleteFormState()->getValue($group_key, $this->configuration['group']); + if (count($groups) > 1) { + $form['defaults'] = [ + '#type' => 'container', + '#prefix' => '
', + '#suffix' => '
', + ]; + foreach($groups as $group) { + $form['defaults'][strtolower(str_replace(' ', '_', $group))] = [ + '#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'], + ]; + } + } + 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; } @@ -161,9 +182,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) > 1) { + return $settings_form['defaults']; + } + else { + return $settings_form['default']; + } } /**