diff --git a/config/install/paragraphs.paragraphs_type.container.yml b/config/install/paragraphs.paragraphs_type.container.yml index 16bce23..b61f940 100644 --- a/config/install/paragraphs.paragraphs_type.container.yml +++ b/config/install/paragraphs.paragraphs_type.container.yml @@ -9,6 +9,3 @@ dependencies: id: container label: 'Container' description: 'Use Container to group paragraphs to apply a common style.' -behavior_plugins: - style: - enabled: true diff --git a/config/schema/paragraphs_collection.schema.yml b/config/schema/paragraphs_collection.schema.yml index 44aa228..348e908 100644 --- a/config/schema/paragraphs_collection.schema.yml +++ b/config/schema/paragraphs_collection.schema.yml @@ -1,12 +1,16 @@ paragraphs.behavior.settings.style: type: paragraphs.behavior.settings_base mapping: - group: - type: string - label: Style group - default: - type: string - label: Default style + groups: + type: sequence + label: 'Style groups' + sequence: + type: mapping + label: 'Style groups configuration' + mapping: + default: + type: string + label: 'Default group style' paragraphs.behavior.settings.grid_layout: type: paragraphs.behavior.settings_base diff --git a/modules/paragraphs_collection_demo/paragraphs_collection_demo.install b/modules/paragraphs_collection_demo/paragraphs_collection_demo.install index 1f35529..9983872 100644 --- a/modules/paragraphs_collection_demo/paragraphs_collection_demo.install +++ b/modules/paragraphs_collection_demo/paragraphs_collection_demo.install @@ -82,11 +82,10 @@ function _paragraphs_collection_demo_add_background_plugin_field() { ], 'style' => [ 'enabled' => TRUE, - 'group' => '', + 'groups' => ['general_group' => ['default' => '']], ], ]); $container->save(); - } /** @@ -108,7 +107,7 @@ function _paragraphs_collection_demo_create_demo_article() { 'type' => 'container', 'paragraphs_container_paragraphs' => [$style_text_paragraph], ]); - $style_paragraph->setBehaviorSettings('style', ['style' => 'paragraphs-green']); + $style_paragraph->setBehaviorSettings('style', ['styles' => ['general_group' => 'paragraphs-green']]); $style_paragraph->save(); $paragraphs[] = $style_paragraph; diff --git a/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style.yml b/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style.yml index 52550f5..0b38382 100644 --- a/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style.yml +++ b/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style.yml @@ -2,27 +2,27 @@ paragraphs-green: title: 'Green' description: 'Green background with white font color, text centered.' groups: - - 'General Group' + - general_group libraries: - 'paragraphs_collection_demo/style' paragraphs-blue: title: 'Blue' description: 'Blue background with white font color, text centered.' groups: - - 'General Group' + - general_group libraries: - 'paragraphs_collection_demo/style' paragraphs-slideshow-light: title: 'Slideshow Light' description: 'Light blue background with centered text.' groups: - - 'Slideshow Group' + - slideshow_group libraries: - 'paragraphs_collection_demo/style' paragraphs-slideshow-dark: title: 'Slideshow Dark' description: 'Dark blue background with centered text.' groups: - - 'Slideshow Group' + - slideshow_group libraries: - 'paragraphs_collection_demo/style' diff --git a/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style_group.yml b/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style_group.yml new file mode 100644 index 0000000..379aea7 --- /dev/null +++ b/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style_group.yml @@ -0,0 +1,4 @@ +general_group: + label: 'General Group' +slideshow_group: + label: 'Slideshow Group' diff --git a/modules/paragraphs_collection_demo/src/Tests/ParagraphsCollectionDemoTest.php b/modules/paragraphs_collection_demo/src/Tests/ParagraphsCollectionDemoTest.php index 2899252..725e88a 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->assertOptionSelected('edit-behavior-plugins-style-settings-groups', 'general_group'); + $this->assertNoOptionSelected('edit-behavior-plugins-style-settings-groups', 'slideshow_group'); + $options = $this->xpath('//*[@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'); @@ -95,20 +98,26 @@ class ParagraphsCollectionDemoTest extends ParagraphsExperimentalTestBase { $node->save(); // Use green style for this container. - $paragraph->setBehaviorSettings('style', ['style' => 'paragraphs-green']); + $paragraph->setBehaviorSettings('style', ['styles' => ['general_group' => 'paragraphs-green']]); $paragraph->save(); // Check the applied style on the paragraph. $this->drupalGet('node/' . $node->id()); - $this->assertRaw('class="paragraphs-behavior-style--paragraphs-green paragraphs-behavior-background paragraph paragraph--type--container paragraph--view-mode--default"'); + $this->assertRaw('paragraphs-behavior-background'); + $this->assertRaw('paragraphs-behavior-style--paragraphs-green'); + $this->assertRaw('paragraph--type--container'); + $this->assertRaw('paragraph--view-mode--default'); // Use blue style for the container. - $paragraph->setBehaviorSettings('style', ['style' => 'paragraphs-blue']); + $paragraph->setBehaviorSettings('style', ['styles' => ['general_group' => 'paragraphs-blue']]); $paragraph->save(); // Check that the blue style is applied on the paragraph. $this->drupalGet('node/' . $node->id()); - $this->assertRaw('class="paragraphs-behavior-style--paragraphs-blue paragraphs-behavior-background paragraph paragraph--type--container paragraph--view-mode--default"'); + $this->assertRaw('paragraphs-behavior-background'); + $this->assertRaw('paragraphs-behavior-style--paragraphs-blue'); + $this->assertRaw('paragraph--type--container'); + $this->assertRaw('paragraph--view-mode--default'); } /** diff --git a/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style.yml b/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style.yml index a821fc3..14dfa07 100644 --- a/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style.yml +++ b/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style.yml @@ -2,7 +2,7 @@ regular: title: 'Regular' description: 'Default style.' groups: - - 'Regular Test Group' + - regular_test_group libraries: - 'paragraphs_collection_test/style' classes: @@ -13,29 +13,23 @@ bold: title: 'Bold' description: 'Bold text.' groups: - - 'Bold Test Group' - - 'Italic Test Group' - - 'Underline Test Group' - - 'Regular Test Group' + - bold_test_group + - italic_test_group + - underline_test_group + - regular_test_group libraries: - 'paragraphs_collection_test/style' italic: title: 'Italic' description: 'Italic text.' groups: - - 'Italic Test Group' + - italic_test_group libraries: - 'paragraphs_collection_test/style' underline: title: 'Underline' description: 'Underlined text.' groups: - - 'Underline Test Group' - libraries: - - 'paragraphs_collection_test/style' -groupless: - title: 'Groupless' - description: 'A test style that does not belong to any style groups.' - groups: [ ] + - underline_test_group libraries: - 'paragraphs_collection_test/style' diff --git a/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style_group.yml b/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style_group.yml new file mode 100644 index 0000000..ba24d0a --- /dev/null +++ b/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style_group.yml @@ -0,0 +1,8 @@ +regular_test_group: + label: 'Regular Test Group' +bold_test_group: + label: 'Bold Test Group' +italic_test_group: + label: 'Italic Test Group' +underline_test_group: + label: 'Underline Test Group' diff --git a/paragraphs_collection.install b/paragraphs_collection.install index e43c56c..dbf8213 100644 --- a/paragraphs_collection.install +++ b/paragraphs_collection.install @@ -4,6 +4,8 @@ * Installation hooks for paragraphs collection module. */ +use Drupal\paragraphs\Entity\ParagraphsType; + /** * Add default config for enabled styles. */ @@ -13,3 +15,51 @@ function paragraphs_collection_update_8001() { ->set('enabled_styles', []) ->save(); } + +/** + * Update style plugin config of Paragraph types. + */ +function paragraphs_collection_update_8002() { + // Clear group and style collection caches. + \Drupal::cache('discovery')->deleteMultiple(['paragraphs_collection_style', 'paragraphs_collection_style_group']); + // Get all groups id. + $groups = \Drupal::service('paragraphs_collection.style_discovery')->getStyleGroups(); + $group_ids = []; + foreach ($groups as $group) { + $group_ids[$group->getUntranslatedString()] = $group['label']; + } + $config_factory = \Drupal::configFactory(); + // Loop over all paragraph types. + foreach ($config_factory->listAll('paragraphs.paragraphs_type.') as $name) { + $paragraph_type = $config_factory->getEditable($name); + if ($paragraph_type->get('behavior_plugins.style')) { + $default = ''; + // Get the old configuration if the style plugin was enabled. + $group_label = $paragraph_type->get('behavior_plugins.style.group'); + if (!empty($group_label) && isset($group_ids[$group_label])) { + $default_style = $paragraph_type->get('behavior_plugins.style.default'); + if ($style = \Drupal::service('paragraphs_collection.style_discovery')->getStyle($default_style)) { + if (in_array($group_ids[$group_label], $style['groups'])) { + $default = $default_style; + } + } + $paragraph_type->clear('behavior_plugins.style'); + $paragraph_type->set('behavior_plugins.style.enabled', TRUE); + $paragraph_type->set('behavior_plugins.style.groups.' . $group_ids[$group_label] . '.default', $default); + $paragraph_type->save(); + } + elseif (!empty($default_style = $paragraph_type->get('behavior_plugins.style.default'))) { + // If there is a default style configured, then set the its group as + // default config. + if ($style = \Drupal::service('paragraphs_collection.style_discovery')->getStyle($default_style)) { + if (!empty($style['groups']) && isset($groups[reset($style['groups'])])) { + $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 --git a/paragraphs_collection.module b/paragraphs_collection.module index 41f4a0b..998a354 100644 --- a/paragraphs_collection.module +++ b/paragraphs_collection.module @@ -23,8 +23,10 @@ function paragraphs_collection_paragraph_access(EntityInterface $entity, $operat function paragraphs_collection_theme_suggestions_paragraph(array $variables) { $suggestions = []; $paragraph = $variables['elements']['#paragraph']; - if ($style = ParagraphsStylePlugin::getStyleTemplate($paragraph)) { - $suggestions[] = 'paragraph__' . $paragraph->bundle() . '__' . $style; + if ($templates = ParagraphsStylePlugin::getStylesTemplate($paragraph)) { + foreach ($templates as $template) { + $suggestions[] = 'paragraph__' . $paragraph->bundle() . '__' . $template; + } return $suggestions; } } @@ -68,7 +70,7 @@ function paragraphs_collection_theme() { * * Implements hook_modules_installed(). */ function paragraphs_collection_modules_installed($modules) { - \Drupal::cache('discovery')->deleteMultiple(['paragraphs_collection_grid_layouts', 'paragraphs_collection_style']); + \Drupal::cache('discovery')->deleteMultiple(['paragraphs_collection_grid_layouts', 'paragraphs_collection_style', 'paragraphs_collection_style_group']); } /** diff --git a/src/Controller/OverviewController.php b/src/Controller/OverviewController.php index 281278c..43a7c6d 100644 --- a/src/Controller/OverviewController.php +++ b/src/Controller/OverviewController.php @@ -200,7 +200,7 @@ class OverviewController extends ControllerBase { /** @var ParagraphsType $paragraphs_type */ $configuration = $paragraphs_type->getBehaviorPlugin('style')->getConfiguration(); if (isset($configuration['enabled']) && $configuration['enabled']) { - $styles_grouped_by_paragraphs_types[$paragraph_type_id] = $configuration['group']; + $styles_grouped_by_paragraphs_types[$paragraph_type_id] = array_keys($configuration['groups']); } } @@ -214,9 +214,12 @@ class OverviewController extends ControllerBase { $paragraphs_types_grouped_by_styles = []; foreach ($styles as $style_id => $style) { $paragraphs_types_grouped_by_styles[$style_id] = []; - foreach ($styles_grouped_by_paragraphs_types as $paragraphs_type_id => $used_style_group) { - $enabled_styles = array_keys($this->styleDiscovery->getStyleOptions($used_style_group)); - if (in_array($style_id, $enabled_styles)) { + foreach ($styles_grouped_by_paragraphs_types as $paragraphs_type_id => $used_style_groups) { + $enabled_styles = []; + foreach ($used_style_groups as $used_style_group) { + $enabled_styles += $this->styleDiscovery->getStyleOptions($used_style_group); + } + if (in_array($style_id, array_keys($enabled_styles))) { $paragraphs_types_grouped_by_styles[$style_id][$paragraphs_type_id] = $paragraphs_types[$paragraphs_type_id]; } } diff --git a/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php index f111814..687d86b 100644 --- a/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php +++ b/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php @@ -10,7 +10,6 @@ use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\paragraphs\Entity\Paragraph; -use Drupal\paragraphs\Entity\ParagraphsType; use Drupal\paragraphs\ParagraphInterface; use Drupal\paragraphs\ParagraphsBehaviorBase; use Drupal\paragraphs_collection\StyleDiscoveryInterface; @@ -81,16 +80,20 @@ 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 -'); + $paragraph_styles = $this->getStyles($paragraph); + foreach (array_keys($this->configuration['groups']) as $group_id) { + $group_default = $this->configuration['groups'][$group_id]['default']; + $form['style_wrapper']['styles'][$group_id] = [ + '#type' => 'select', + '#title' => $this->t('%group Style', ['%group' => $this->yamlStyleDiscovery->getGroupLabel($group_id)]), + '#options' => $this->getStyleOptions($group_id, $group_default), + '#default_value' => !empty($paragraph_styles[$group_id]) ? $paragraph_styles[$group_id] : $group_default, + '#attributes' => ['class' => ['paragraphs-style']], + ]; + // Allow empty option in case there is no default style configured. + if (empty($group_default)) { + $form['style_wrapper']['styles'][$group_id]['#empty_option'] = $this->t('- Default -'); + } } return $form; @@ -129,31 +132,43 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $form['group'] = [ + $form['groups'] = [ '#type' => 'select', - '#empty_option' => $this->t('- None -'), + '#title' => $this->t('Style groups'), + '#multiple' => TRUE, '#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'], + '#description' => $this->t('Restrict available styles to certain style groups.'), + '#default_value' => array_keys($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'], + $group_key = ['behavior_plugins', $this->getPluginId(), 'settings', 'groups']; + $groups = $form_state->getCompleteFormState()->getValue($group_key, $this->configuration['groups']); + $form['groups_defaults'] = [ + '#type' => 'container', '#prefix' => '
', '#suffix' => '
', ]; + + foreach (array_keys($groups) as $group_id) { + $default = ''; + if (!empty($this->configuration['groups'][$group_id]['default'])) { + $default = $this->configuration['groups'][$group_id]['default']; + } + $group_label = $this->yamlStyleDiscovery->getGroupLabel($group_id); + $form['groups_defaults'][$group_id]['default'] = [ + '#type' => 'select', + '#title' => $this->t('@label default style', ['@label' => $group_label]), + '#empty_option' => $this->t('- None -'), + '#options' => $this->yamlStyleDiscovery->getStyleOptions($group_id), + '#description' => $this->t('Default option for the @label group on a behavior form.', ['@label' => $group_label]), + '#default_value' => $default, + ]; + } + return $form; } @@ -164,7 +179,7 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF $group_select = $form_state->getTriggeringElement(); // Gets the behavior plugin settings form. $settings_form = NestedArray::getValue($form, array_slice($group_select['#array_parents'], 0, -1)); - return $settings_form['default']; + return $settings_form['groups_defaults']; } /** @@ -175,14 +190,16 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF if (empty($this->yamlStyleDiscovery->getStyleGroups())) { $form_state->setErrorByName('message', $this->t('There is no style group available, the style plugin can not be enabled.')); } + if (!$form_state->getValue('groups')) { + $form_state->setErrorByName('groups', $this->t('The style plugin cannot be enabled if no groups are selected.')); + } } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { - $this->configuration['group'] = $form_state->getValue('group'); - $this->configuration['default'] = $form_state->getValue('default'); + $this->configuration['groups'] = $form_state->getValue('groups_defaults'); } /** @@ -190,8 +207,7 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF */ public function defaultConfiguration() { return [ - 'group' => '', - 'default' => '', + 'groups' => [], ]; } @@ -199,36 +215,45 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF * {@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_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']); + // Get all config for all styles and if it is not set, fallback to the + // default style. If default style is set to none, no style will be applied. + $paragraph_styles = $this->getStyles($paragraph); + foreach ($this->configuration['groups'] as $key => $value) { + $style = $this->yamlStyleDiscovery->getStyle($paragraph_styles[$key], $this->configuration['groups'][$key]['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']); + } } } } - /** - * {@inheritdoc} - */ - public function settingsSummary(Paragraph $paragraph) { - $styles = $this->yamlStyleDiscovery->getStyleOptions(); - if ($style = $paragraph->getBehaviorSetting($this->getPluginId(), 'style')) { - if ($style != $this->configuration['default']) { - return [$this->t('Style: @style', ['@style' => $styles[$style]])]; - } - } - return []; - } + /** + * {@inheritdoc} + */ + public function settingsSummary(Paragraph $paragraph) { + $style_options = $this->yamlStyleDiscovery->getStyleOptions(); + $summary = []; + if ($styles = $paragraph->getBehaviorSetting($this->getPluginId(), 'styles')) { + foreach ($styles as $group_id => $style) { + if (!isset($this->configuration['groups'][$group_id]) || $style != $this->configuration['groups'][$group_id]['default']) { + $summary[] = $this->t('@group: @style', [ + '@group' => $this->yamlStyleDiscovery->getGroupLabel($group_id), + '@style' => $style_options[$style] + ]); + } + } + return !empty($summary) ? [implode($summary, ', ')] : []; + } + return []; + } /** * Ajax callback for loading the style description for the currently @@ -264,20 +289,57 @@ class ParagraphsStylePlugin extends ParagraphsBehaviorBase implements ContainerF * @param \Drupal\paragraphs\ParagraphInterface $paragraph * The paragraph. * - * @return string + * @return string[] * The template setting of the selected style. */ - public static function getStyleTemplate(ParagraphInterface $paragraph) { + public static function getStylesTemplate(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']; + $templates = []; + $paragraph_styles = $paragraph->getParagraphType()->getBehaviorPlugin('style')->getStyles($paragraph); + foreach ($paragraph_styles as $group_name => $paragraph_style) { + if ($style = \Drupal::service('paragraphs_collection.style_discovery')->getStyle($paragraph_style)) { + if (!empty($style['template'])) { + $templates[] = $style['template']; + } + } + } + return $templates; + } + return NULL; + } + + /** + * Gets the current styles for each enabled group. + * + * @param \Drupal\paragraphs\ParagraphInterface $paragraph + * The paragraph. + * + * @return string[] + * The list of all enabled groups with their configured style. + */ + public function getStyles(ParagraphInterface $paragraph) { + $paragraph_styles = []; + foreach ($this->configuration['groups'] as $key => $value) { + $paragraph_styles[$key] = $value['default']; + } + if ($styles = $paragraph->getBehaviorSetting('style', 'styles')) { + // Loop over all groups to get the behavior setting. + foreach ($styles as $key => $value) { + $paragraph_styles[$key] = $value; + } + } + elseif ($style_config = $paragraph->getBehaviorSetting('style', 'style')) { + // If there is old config, map it to the current one. + 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'); + } } - }; + } } + + return $paragraph_styles; } } diff --git a/src/StyleDiscovery.php b/src/StyleDiscovery.php index aae5828..476ce74 100644 --- a/src/StyleDiscovery.php +++ b/src/StyleDiscovery.php @@ -102,7 +102,7 @@ class StyleDiscovery implements StyleDiscoveryInterface { $enabled_styles = $this->configFactory->get('paragraphs_collection.settings')->get('enabled_styles'); foreach ($style_collection as $style => $definition) { if (empty($enabled_styles) || in_array($style, $enabled_styles)) { - if ((empty($group) || in_array($group, $definition['groups']))) { + if (empty($group) || in_array($group, $definition['groups'])) { $options[$style] = $definition['title']; } } @@ -165,41 +165,69 @@ class StyleDiscovery implements StyleDiscoveryInterface { } /** + * Gets the YAML discovery. + * + * @return \Drupal\Core\Discovery\YamlDiscovery + * The YAML discovery. + */ + protected function getYamlDiscovery() { + return new YamlDiscovery('paragraphs.style', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories()); + } + + /** + * {@inheritdoc} + */ + public function getLibraries($style) { + $collection = $this->getStyles(); + return $collection[$style]['libraries']; + } + + /** * {@inheritdoc} */ public function getStyleGroups() { + $cache_id = 'paragraphs_collection_style_group'; if ($this->groupCollection !== NULL) { return $this->groupCollection; } - else if (!empty($styles = $this->getStyles())) { - foreach ($styles as $style => $definition) { - foreach ($definition['groups'] as $group) { - $this->groupCollection[$group] = $group; - } - } + else if ($cached = $this->cache->get($cache_id)) { + $this->groupCollection = $cached->data; } else { + $yaml_discovery = $this->getYamlGroupDiscovery(); $this->groupCollection = []; + foreach ($yaml_discovery->findAll() as $module => $groups) { + foreach ($groups as $group => $definition) { + if (empty($definition['label'])) { + throw new InvalidStyleException('The "label" of "' . $group . '" must be non-empty.'); + } + $this->groupCollection[$group] = $this->t($definition['label']); + } + } + $this->cache->set($cache_id, $this->groupCollection); } return $this->groupCollection; } /** - * Gets the YAML discovery. - * - * @return \Drupal\Core\Discovery\YamlDiscovery - * The YAML discovery. + * {@inheritdoc} */ - protected function getYamlDiscovery() { - return new YamlDiscovery('paragraphs.style', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories()); + public function getGroupLabel($group_id) { + $groups = $this->getStyleGroups(); + if (in_array($group_id, array_keys($groups))) { + return $groups[$group_id]; + } + return NULL; } /** - * {@inheritdoc} + * Gets the YAML group discovery. + * + * @return \Drupal\Core\Discovery\YamlDiscovery + * The YAML discovery. */ - public function getLibraries($style) { - $collection = $this->getStyles(); - return $collection[$style]['libraries']; + protected function getYamlGroupDiscovery() { + return new YamlDiscovery('paragraphs.style_group', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories()); } } diff --git a/src/StyleDiscoveryInterface.php b/src/StyleDiscoveryInterface.php index bb1d0de..f3c83cf 100644 --- a/src/StyleDiscoveryInterface.php +++ b/src/StyleDiscoveryInterface.php @@ -61,4 +61,15 @@ interface StyleDiscoveryInterface { */ public function getStyle($style, $default = NULL); + /** + * Gets group label. + * + * @param string $group_id + * The group id. + * + * @return string + * The translatable group label. + */ + public function getGroupLabel($group_id); + } diff --git a/src/Tests/ParagraphsCollectionOverviewTest.php b/src/Tests/ParagraphsCollectionOverviewTest.php index fbe9c68..bf5a9f3 100644 --- a/src/Tests/ParagraphsCollectionOverviewTest.php +++ b/src/Tests/ParagraphsCollectionOverviewTest.php @@ -84,13 +84,6 @@ class ParagraphsCollectionOverviewTest extends ParagraphsExperimentalTestBase { $this->assertLink('Layouts'); $this->assertLink('Styles'); - // Check that the groupless style from Paragraph Collection Test is being - // displayed in the styles overview. - $this->drupalGet('/admin/reports/paragraphs_collection/styles'); - $this->assertText('Groupless'); - $this->assertText('groupless'); - $this->assertText('A test style that does not belong to any style groups.'); - // Disable the grid layout and style plugins for all paragraphs types. $paragraph_type_ids = \Drupal::entityQuery('paragraphs_type')->execute(); $paragraphs_types = ParagraphsType::loadMultiple($paragraph_type_ids); diff --git a/src/Tests/ParagraphsStylePluginTest.php b/src/Tests/ParagraphsStylePluginTest.php index 78f6c86..0df5d48 100644 --- a/src/Tests/ParagraphsStylePluginTest.php +++ b/src/Tests/ParagraphsStylePluginTest.php @@ -48,12 +48,20 @@ 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][default]', ''); + $this->assertFieldByName('behavior_plugins[style][settings][groups][]'); $edit = [ 'behavior_plugins[style][enabled]' => TRUE, - 'behavior_plugins[style][settings][group]' => '', - 'behavior_plugins[style][settings][default]' => '', + 'behavior_plugins[style][settings][groups][]' => [], + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + $this->assertText('The style plugin cannot be enabled if no groups are selected.'); + $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][groups_defaults][regular_test_group][default]' => '', ]; $this->drupalPostForm(NULL, $edit, t('Save')); @@ -63,43 +71,31 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase { // Check that we have style plugin. $this->assertText('Style'); - $this->assertField('paragraphs[0][behavior_plugins][style][style_wrapper][style]'); - - // 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.'); + $this->assertField('paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]'); // 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]); + $this->assertEqual('Regular', $styles[0]->option[2]); // 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->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['italic_test_group'], + 'behavior_plugins[style][settings][groups_defaults][italic_test_group][default]' => '', ]; $this->drupalPostForm(NULL, $edit, t('Save')); // 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]'); - $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 not available.'); // 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']); @@ -110,20 +106,21 @@ 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][groups_defaults][italic_test_group][default]', ''); $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][groups_defaults][regular_test_group][default]' => 'regular', ]; $this->drupalPostForm(NULL, $edit, t('Save')); // Regular style should be shown first in the list. $this->drupalGet('node/add/paragraphed_test'); $this->drupalPostAjaxForm(NULL, [], 'paragraphs_test_style_plugin_add_more'); + $this->assertOptionSelectedWithDrupalSelector('edit-paragraphs-0-behavior-plugins-style-style-wrapper-styles-regular-test-group', 'regular'); $styles = $this->xpath('//select[contains(@id, :id)]', [':id' => 'edit-paragraphs-0-behavior-plugins-style-style']); $this->assertEqual(2, count($styles[0]->option)); $this->assertEqual('- Regular -', $styles[0]->option[0]); @@ -145,7 +142,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][styles][regular_test_group]', 'regular'); } /** @@ -189,30 +186,41 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase { $node->save(); // Check the empty summary. + $behavior_plugins = $paragraph->getParagraphType()->get('behavior_plugins'); + $behavior_plugins['style'] = [ + 'enabled' => TRUE, + 'groups' => ['bold_test_group' => ['default' => '']], + ]; + $paragraph->getParagraphType()->set('behavior_plugins', $behavior_plugins); + $paragraph->getParagraphType()->save(); $style_plugin = $paragraph->getParagraphType()->getEnabledBehaviorPlugins()['style']; $this->assertEqual([], $style_plugin->settingsSummary($paragraph)); // Use bold style for this container. - $paragraph->setBehaviorSettings('style', ['style' => 'bold']); + $paragraph->setBehaviorSettings('style', ['styles' => ['bold_test_group' => 'bold']]); $paragraph->save(); - $this->assertEqual(['Style: Bold'], $style_plugin->settingsSummary($paragraph)); + $this->assertEqual(['Bold Test Group: Bold'], $style_plugin->settingsSummary($paragraph)); // Check the settings summary in a closed mode. $this->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed'); $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertRaw('edit-field-paragraphs-0-top-icons'); - $this->assertRaw('Test text 1., Style: Bold'); + $this->assertRaw('Test text 1., Bold Test Group: Bold'); // Configure style bold as default. $edit = [ - 'behavior_plugins[style][settings][group]' => '', - 'behavior_plugins[style][settings][default]' => 'bold', + 'behavior_plugins[style][settings][groups][]' => ['bold_test_group'], + ]; + $this->drupalPostAjaxForm('admin/structure/paragraphs_type/' . $paragraph->getType(), $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['bold_test_group'], + 'behavior_plugins[style][settings][groups_defaults][bold_test_group][default]' => 'bold', ]; - $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph->getType(), $edit, t('Save')); + $this->drupalPostForm(NULL, $edit, t('Save')); // Check that the settings summary does not show the default style. $this->drupalGet('node/' . $node->id() . '/edit'); - $this->assertRaw('Test text 1.'); + $this->assertRaw('
Test text 1.
'); $this->assertNoRaw('Style: Bold'); $this->assertNoRaw('Style: - Bold -'); } @@ -234,7 +242,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,36 +270,52 @@ 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, + 'behavior_plugins[style][settings][groups][]' => ['bold_test_group'], + ]; + $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); $edit = [ 'behavior_plugins[style][enabled]' => TRUE, + 'behavior_plugins[style][settings][groups][]' => ['bold_test_group'], ]; $this->drupalPostForm(NULL, $edit, t('Save')); + $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); // Assert global settings. $this->drupalGet('admin/reports/paragraphs_collection/styles'); $this->assertFieldByName('styles[bold][enabled]', FALSE); - $this->assertFieldByName('styles[groupless][enabled]', FALSE); $this->assertFieldByName('styles[italic][enabled]', FALSE); $this->assertFieldByName('styles[regular][enabled]', FALSE); $this->assertFieldByName('styles[underline][enabled]', FALSE); // Add a node with paragraphs and check the available styles. $this->drupalGet('node/add/paragraphed_test'); - $this->drupalPostAjaxForm(NULL, NULL, 'paragraphs_container_add_more'); + $this->drupalPostAjaxForm(NULL, NULL, 'paragraphs_' . $paragraph_type . '_add_more'); $options = $this->xpath('//*[contains(@class,"paragraphs-style")]/option'); - $this->assertEqual(6, count($options)); + $this->assertEqual(2, count($options)); $edit = [ 'title[0][value]' => 'global_settings', - 'paragraphs[0][behavior_plugins][style][style_wrapper][style]' => 'bold' + 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][bold_test_group]' => 'bold' ]; $this->drupalPostForm(NULL, $edit, 'Save'); $this->assertRaw('paragraphs-behavior-style--bold'); + $edit = [ + 'behavior_plugins[style][enabled]' => TRUE, + 'behavior_plugins[style][settings][groups][]' => ['italic_test_group'], + ]; + $this->drupalPostAjaxForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][enabled]' => TRUE, + 'behavior_plugins[style][settings][groups][]' => ['italic_test_group'], + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + // Update global settings and enable two styles. $this->drupalGet('admin/reports/paragraphs_collection/styles'); $edit = [ - 'styles[groupless][enabled]' => TRUE, 'styles[italic][enabled]' => TRUE, ]; $this->drupalPostForm(NULL, $edit, 'Save configuration'); @@ -301,18 +326,12 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase { $this->clickLink('Edit'); // Assert that only the two enabled styles are available. $options = $this->xpath('//*[contains(@class,"paragraphs-style")]/option'); - $this->assertEqual(3, count($options)); + $this->assertEqual(2, count($options)); $this->assertEqual($options[0], '- Default -'); - $this->assertEqual($options[1], 'Groupless'); - $this->assertEqual($options[2], 'Italic'); + $this->assertEqual($options[1], 'Italic'); - // Check that styles are not shown in style config when disabled. - $edit = [ - 'behavior_plugins[style][settings][group]' => '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][groups_defaults][italic_test_group][default]")]/option'); $this->assertEqual(2, count($options)); $this->assertEqual($options[0], '- None -'); $this->assertEqual($options[1], 'Italic'); @@ -324,11 +343,22 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase { ]; $this->drupalPostForm('admin/reports/paragraphs_collection/styles', $edit, 'Save configuration'); // Set default style to italic. - $this->drupalGet('admin/structure/paragraphs_type/container'); - $this->drupalPostForm(NULL, ['behavior_plugins[style][settings][default]' => 'italic'], t('Save')); + $edit = [ + 'behavior_plugins[style][enabled]' => TRUE, + 'behavior_plugins[style][settings][groups][]' => ['italic_test_group'], + ]; + $this->drupalPostAjaxForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => ['italic_test_group'], + 'behavior_plugins[style][settings][groups_defaults][italic_test_group][default]' => 'italic', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); // Set the paragraph style to bold. - $this->drupalPostForm('node/' . $node->id() . '/edit', ['paragraphs[0][behavior_plugins][style][style_wrapper][style]' => 'bold'], t('Save')); + $this->drupalPostForm('node/' . $node->id() . '/edit', ['paragraphs[0][behavior_plugins][style][style_wrapper][styles][italic_test_group]' => 'bold'], t('Save')); $this->assertRaw('paragraphs-behavior-style--bold'); + // Assert that the selection is correctly displayed. + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->assertOptionSelected('edit-paragraphs-0-behavior-plugins-style-style-wrapper-styles-italic-test-group', 'bold'); // Disable the bold style. $this->drupalGet('admin/reports/paragraphs_collection/styles'); @@ -341,4 +371,160 @@ class ParagraphsStylePluginTest extends ParagraphsExperimentalTestBase { $this->assertRaw('paragraphs-behavior-style--italic'); } + /** + * Tests the multiple 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); + + // Restrict the paragraphs type to the "Italic Test Group" style group. + $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); + $edit = [ + 'behavior_plugins[style][enabled]' => TRUE, + '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][groups_defaults][italic_test_group][default]' => 'italic', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + // Create a paragraphed test node and check the style classes. + $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 two groups and set their defaults. + $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][groups_defaults][regular_test_group][default]' => 'regular', + 'behavior_plugins[style][settings][groups_defaults][italic_test_group][default]' => 'italic', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + // Check the selects elements for each enabled group and check the classes. + $node = $this->getNodeByTitle('title_to_remember'); + $this->drupalGet('node/' . $node->id() . '/edit'); + $styles = $this->xpath('//select[contains(@name, :name)]', [':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][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][styles][italic_test_group]']); + $this->assertEqual(2, count($styles[0]->option)); + $this->assertEqual('- Italic -', $styles[0]->option[0]); + $this->assertEqual('Bold', $styles[0]->option[1]); + $this->drupalGet('node/' . $node->id()); + $this->assertRaw('paragraphs-behavior-style--italic'); + $this->assertRaw('paragraphs-behavior-style--regular'); + + // 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][groups_defaults][regular_test_group][default]' => 'bold', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + // Check that there is only one select and only one style class. + $this->drupalGet('node/' . $node->id() . '/edit'); + $styles = $this->xpath('//select[contains(@name, :name)]', [':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]']); + $this->assertEqual(2, count($styles[0]->option)); + $this->assertEqual('- Bold -', $styles[0]->option[0]); + $this->assertEqual('Regular', $styles[0]->option[1]); + $styles = $this->xpath('//select[contains(@name, :name)]', [':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][italic_test_group]']); + $this->assertEqual([], $styles); + $this->drupalGet('node/' . $node->id()); + $this->assertNoRaw('paragraphs-behavior-style--italic'); + $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][]' => [ + 'italic_test_group', + 'regular_test_group', + 'underline_test_group' + ], + ]; + $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); + $edit = [ + 'behavior_plugins[style][settings][groups][]' => [ + 'italic_test_group', + 'regular_test_group', + 'underline_test_group' + ], + 'behavior_plugins[style][settings][groups_defaults][italic_test_group][default]' => 'italic', + 'behavior_plugins[style][settings][groups_defaults][regular_test_group][default]' => 'regular', + 'behavior_plugins[style][settings][groups_defaults][underline_test_group][default]' => 'underline', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + // Check that there is only one select and only one style class. + $this->drupalGet('node/' . $node->id() . '/edit'); + $styles = $this->xpath('//select[contains(@name, :name)]', [':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][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][styles][italic_test_group]']); + $this->assertEqual(2, count($styles[0]->option)); + $this->assertEqual('- Italic -', $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][styles][underline_test_group]']); + $this->assertEqual(2, count($styles[0]->option)); + $this->assertEqual('- Underline -', $styles[0]->option[0]); + $this->assertEqual('Bold', $styles[0]->option[1]); + $this->drupalGet('node/' . $node->id()); + $this->assertRaw('paragraphs-behavior-style--italic'); + $this->assertRaw('paragraphs-behavior-style--regular'); + $this->assertRaw('paragraphs-behavior-style--underline'); + + // Change a plugin. + $this->drupalGet('node/' . $node->id() . '/edit'); + $edit = [ + 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]' => 'bold' + ]; + $this->drupalPostForm(NULL, $edit, 'Save'); + $this->assertRaw('paragraphs-behavior-style--italic'); + $this->assertRaw('paragraphs-behavior-style--bold'); + $this->assertRaw('paragraphs-behavior-style--underline'); + } + } diff --git a/style_group_translation_patterns.yml b/style_group_translation_patterns.yml new file mode 100644 index 0000000..1fd0434 --- /dev/null +++ b/style_group_translation_patterns.yml @@ -0,0 +1,4 @@ +translation_patterns: + - matches: '*.style_group.yml' + translatable_keys: + - label