diff -u b/paragraphs_collection.install b/paragraphs_collection.install --- b/paragraphs_collection.install +++ b/paragraphs_collection.install @@ -46,19 +46,14 @@ $paragraph_type->set('behavior_plugins.style.enabled', TRUE); $paragraph_type->set('behavior_plugins.style.groups.' . $group_ids[$group] . '.default', $default); $paragraph_type->save(); - - $paragraphs = \Drupal::entityTypeManager()->getStorage('paragraph')->loadByProperties(['type' => $paragraph_type->get('id')]); - foreach ($paragraphs as $id => $paragraph) { - $style_name = $paragraph->getBehaviorSetting('style', 'style'); - $style_discovery = \Drupal::service('paragraphs_collection.style_discovery'); - $style = $style_discovery->getStyle($style_name); - if (!isset($style['groups'])) { - continue; - } - $group_styles = $style_discovery->getStyleOptions($group_ids[$group]); - if (in_array($style_name, array_keys($group_styles))) { - $paragraph->setBehaviorSettings('style', ['styles' => [reset($style['groups']) => $style_name]]); - $paragraph->save(); + } + elseif (!empty($default_style = $paragraph_type->get('behavior_plugins.style.default'))) { + if ($style = \Drupal::service('paragraphs_collection.style_discovery')->getStyle($default_style)) { + if (in_array(reset($style['groups']), array_values($group_ids))) { + $paragraph_type->clear('behavior_plugins.style'); + $paragraph_type->set('behavior_plugins.style.enabled', TRUE); + $paragraph_type->set('behavior_plugins.style.groups.' . reset($style['groups']) . '.default', $default_style); + $paragraph_type->save(); } } } 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 @@ -80,6 +80,7 @@ 'id' => $wrapper_id, ], ]; + $paragraph_styles = $this->getStyles($paragraph); foreach ($this->configuration['groups'] as $group_id => $default) { $plugin_default = ''; if (isset($this->configuration['groups'][$group_id]['default'])) { @@ -89,7 +90,7 @@ '#type' => 'select', '#title' => $this->t('%group Style', ['%group' => $this->yamlStyleDiscovery->getGroupLabel($group_id)]), '#options' => $this->getStyleOptions($group_id, $plugin_default), - '#default_value' => $paragraph->getBehaviorSetting($this->getPluginId(), ['styles', $group_id], $this->configuration['groups'][$group_id]['default']), + '#default_value' => $paragraph_styles[$group_id], '#attributes' => ['class' => ['paragraphs-style']], ]; // Allow empty option in case there is no default style configured. @@ -213,18 +214,46 @@ ]; } + public function getStyles($paragraph) { + $paragraph_styles = []; + if ($styles = $paragraph->getBehaviorSetting('style', 'styles')) { + foreach ($this->configuration['groups'] as $key => $value) { + $paragraph_styles[$key] = $paragraph->getBehaviorSetting($this->getPluginId(), ['styles', $key]); + } + } + elseif ($style_config = $paragraph->getBehaviorSetting('style', 'style')) { + if ($style = $this->yamlStyleDiscovery->getStyle($style_config)) { + foreach ($this->configuration['groups'] as $key => $value) { + if (in_array($key, $style['groups'])) { + $paragraph_styles[$key] = $paragraph->getBehaviorSetting($this->getPluginId(), 'style'); + } + else { + $paragraph_styles[$key] = ''; + } + } + } + } + else { + foreach ($this->configuration['groups'] as $key => $value) { + $paragraph_styles[$key] = ''; + } + } + return $paragraph_styles; + } + /** * {@inheritdoc} */ 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_styles = []; + $paragraph_styles = $this->getStyles($paragraph); foreach ($this->configuration['groups'] as $key => $value) { - $paragraph_styles[$key] = $paragraph->getBehaviorSetting($this->getPluginId(), ['styles', $key], $this->configuration['groups'][$key]['default']); - } - foreach ($paragraph_styles as $key => $value) { - $style = $this->yamlStyleDiscovery->getStyle($value, $this->configuration['groups'][$key]['default']); + $plugin_default = ''; + if (isset($this->configuration['groups'][$key]['default'])) { + $plugin_default = $this->configuration['groups'][$key]['default']; + } + $style = $this->yamlStyleDiscovery->getStyle($paragraph_styles[$key], $plugin_default); if ($style) { $build['#attributes']['class'][] = 'paragraphs-behavior-' . $this->getPluginId() . '--' . $style['name']; if (!isset($build['#attached']['library'])) {