diff -u b/config/schema/paragraphs_collection.schema.yml b/config/schema/paragraphs_collection.schema.yml --- b/config/schema/paragraphs_collection.schema.yml +++ b/config/schema/paragraphs_collection.schema.yml @@ -1,24 +1,19 @@ paragraphs.behavior.settings.style: type: paragraphs.behavior.settings_base mapping: - group: - type: string - label: Style group groups: type: sequence label: 'Style groups' sequence: - type: string - label: 'Group name' + type: mapping + label: 'Style groups configuration' + mapping: + default: + type: string + label: 'Default group style' default: type: string label: Default style - defaults: - type: sequence - label: 'Default styles' - sequence: - type: string - label: 'Style name' paragraphs.behavior.settings.grid_layout: type: paragraphs.behavior.settings_base 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 @@ -10,10 +10,10 @@ 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; +use Drupal\paragraphs_collection\StyleGroupDiscoveryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -37,6 +37,13 @@ */ protected $yamlStyleDiscovery; + /** + * The yaml style group discovery. + * + * @var \Drupal\paragraphs_collection\StyleGroupDiscovery + */ + protected $yamlStyleGroupDiscovery; + /** * Constructs a new SelectionBase object. * @@ -50,10 +57,13 @@ * The entity field manager. * @param \Drupal\paragraphs_collection\StyleDiscoveryInterface $yaml * The yaml style discovery. + * @param \Drupal\paragraphs_collection\StyleGroupDiscoveryInterface $style_group_discovery + * The yaml style group discovery. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, StyleDiscoveryInterface $yaml) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, StyleDiscoveryInterface $yaml, StyleGroupDiscoveryInterface $style_group_discovery) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager); $this->yamlStyleDiscovery = $yaml; + $this->yamlStyleGroupDiscovery = $style_group_discovery; } /** @@ -62,7 +72,8 @@ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_field.manager'), - $container->get('paragraphs_collection.style_discovery') + $container->get('paragraphs_collection.style_discovery'), + $container->get('paragraphs_collection.style_group_discovery') ); } @@ -81,38 +92,21 @@ 'id' => $wrapper_id, ], ]; - - 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 -'); - } + foreach ($this->configuration['groups'] as $group_id => $default) { + $plugin_default = ''; + if (isset($this->configuration['groups'][$group_id]['default'])) { + $plugin_default = $this->configuration['groups'][$group_id]['default']; } - } - else { - $form['style_wrapper']['style'] = [ + $form['style_wrapper']['styles'][$group_id] = [ '#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' => $this->t('%group Style', ['%group' => $this->yamlStyleGroupDiscovery->getGroupLabel($group_id)]), + '#options' => $this->getStyleOptions($group_id, $plugin_default), + '#default_value' => $paragraph->getBehaviorSetting($this->getPluginId(), $group_id, $this->configuration['groups'][$group_id]['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 -'); + if (empty($plugin_default)) { + $form['style_wrapper']['styles'][$group_id]['#empty_option'] = $this->t('- Default -'); } } @@ -155,11 +149,11 @@ $form['groups'] = [ '#type' => 'select', '#title' => $this->t('Style groups'), + '#required' => TRUE, '#multiple' => TRUE, - '#empty_option' => $this->t('- None -'), - '#options' => $this->yamlStyleDiscovery->getStyleGroups(), - '#description' => $this->t('Restrict available styles to a certain style group. Select "- None -" to allow all styles.'), - '#default_value' => $this->configuration['groups'], + '#options' => $this->yamlStyleGroupDiscovery->getStyleGroups(), + '#description' => $this->t('Restrict available styles to a certain style group. Select none to allow all styles.'), + '#default_value' => array_keys($this->configuration['groups']), '#ajax' => [ 'callback' => [$this, 'updateDefaultStyle'], 'wrapper' => 'style-wrapper', @@ -168,38 +162,25 @@ // @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) != 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, - ]; + $form['groups_defaults'] = [ + '#type' => 'container', + '#prefix' => '
', + '#suffix' => '
', + ]; + + foreach ($groups as $group_id => $group_default) { + $default = ''; + if (!empty($this->configuration['groups'][$group_id]['default'])) { + $default = $this->configuration['groups'][$group_id]['default']; } - } - else { - $form['default'] = [ + $group_label = $this->yamlStyleGroupDiscovery->getGroupLabel($group_id); + $form['groups_defaults'][$group_id]['default'] = [ '#type' => 'select', + '#title' => $this->t($group_label . ' default style'), '#empty_option' => $this->t('- None -'), - '#options' => $this->yamlStyleDiscovery->getStyleOptions(reset($groups)), - '#title' => $this->t('Default style'), + '#options' => $this->yamlStyleDiscovery->getStyleOptions($group_id), '#description' => $this->t('This style will be default option on a behavior form.'), - '#default_value' => $this->configuration['default'], - '#prefix' => '
', - '#suffix' => '
', + '#default_value' => $default, ]; } @@ -211,15 +192,9 @@ */ 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)); - if (count($groups) != 0) { - return $settings_form['defaults']; - } - else { - return $settings_form['default']; - } + return $settings_form['groups_defaults']; } /** @@ -236,10 +211,7 @@ * {@inheritdoc} */ 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['groups'] = $form_state->getValue('groups_defaults'); } /** @@ -247,10 +219,7 @@ */ public function defaultConfiguration() { return [ - 'group' => '', 'groups' => [], - 'default' => '', - 'defaults' => [], ]; } @@ -261,29 +230,11 @@ // 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 = []; - if (!empty($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']); - } - } - } + foreach ($this->configuration['groups'] as $key => $value) { + $paragraph_styles[$key] = $paragraph->getBehaviorSetting($this->getPluginId(), ['styles', $key], $this->configuration['groups'][$key]['default']); } - else { - $paragraph_style = $paragraph->getBehaviorSetting($this->getPluginId(), 'style', $this->configuration['default']); - $style = $this->yamlStyleDiscovery->getStyle($paragraph_style, $this->configuration['default']); + foreach ($paragraph_styles as $key => $value) { + $style = $this->yamlStyleDiscovery->getStyle($value, $this->configuration['groups'][$key]['default']); if ($style) { $build['#attributes']['class'][] = 'paragraphs-behavior-' . $this->getPluginId() . '--' . $style['name']; if (!isset($build['#attached']['library'])) { @@ -297,7 +248,6 @@ } } } - } /** @@ -352,25 +302,13 @@ */ public static function getStyleTemplate(ParagraphInterface $paragraph) { if ($paragraph->getParagraphType()->hasEnabledBehaviorPlugin('style')) { - 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']; - } - }; - } + $defaults = $paragraph->getParagraphType()->getBehaviorPlugin('style')->getConfiguration()['groups']; + if ($paragraph_style = $paragraph->getBehaviorSetting('style', ['styles', key($defaults)], reset($defaults)['default'])) { + $style = \Drupal::service('paragraphs_collection.style_discovery')->getStyle($paragraph_style); + if (!empty($style['template'])) { + return $style['template']; + } + }; } } diff -u b/src/Tests/ParagraphsStylePluginTest.php b/src/Tests/ParagraphsStylePluginTest.php --- b/src/Tests/ParagraphsStylePluginTest.php +++ b/src/Tests/ParagraphsStylePluginTest.php @@ -49,11 +49,15 @@ $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][groups][]'); - $this->assertFieldByName('behavior_plugins[style][settings][default]', ''); $edit = [ 'behavior_plugins[style][enabled]' => TRUE, 'behavior_plugins[style][settings][groups][]' => [], - 'behavior_plugins[style][settings][default]' => '', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + $this->assertText('Style groups field is required.'); + $edit = [ + 'behavior_plugins[style][enabled]' => TRUE, + 'behavior_plugins[style][settings][groups][]' => ['regular_test_group'], ]; $this->drupalPostForm(NULL, $edit, t('Save')); @@ -65,44 +69,22 @@ $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.'); - // 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][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][italic_test_group]'); - $options = $this->xpath('//select[@name=:name]//option[normalize-space(text())=:text]', [ - ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][italic_test_group]', - ':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']); $this->assertEqual(3, count($styles[0]->option)); @@ -114,11 +96,11 @@ $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); $this->assertFieldByName('behavior_plugins[style][settings][defaults][italic_test_group]'); $edit = [ - 'behavior_plugins[style][settings][groups][]' => ['Regular Test Group'], + '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][]' => ['regular_test_group'], 'behavior_plugins[style][settings][defaults][regular_test_group]' => 'regular', ]; $this->drupalPostForm(NULL, $edit, t('Save')); @@ -207,8 +189,8 @@ // Configure style bold as default. $edit = [ - 'behavior_plugins[style][settings][groups][]' => [], - 'behavior_plugins[style][settings][default]' => 'bold', + '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')); @@ -273,7 +255,6 @@ // 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); @@ -293,7 +274,6 @@ // 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'); @@ -306,8 +286,7 @@ $options = $this->xpath('//*[contains(@class,"paragraphs-style")]/option'); $this->assertEqual(3, 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 = [ @@ -344,13 +323,14 @@ // Restrict the paragraphs type to the "Italic Test Group" style group. $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); $edit = [ - 'behavior_plugins[style][settings][groups][]' => ['Italic Test Group'], + '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][defaults][italic_test_group]' => 'italic', + '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. @@ -371,23 +351,23 @@ // 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'], + '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][defaults][regular_test_group]' => 'regular', - 'behavior_plugins[style][settings][defaults][italic_test_group]' => 'italic', + '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][regular_test_group]']); + $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][italic_test_group]']); + $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]); @@ -398,21 +378,21 @@ // Configure Regular as a default style. $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); $edit = [ - 'behavior_plugins[style][settings][groups][]' => ['Regular Test Group'], + '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][defaults][regular_test_group]' => 'bold', + '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][regular_test_group]']); + $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][italic_test_group]']); + $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'); @@ -421,27 +401,27 @@ // 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'], + '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][defaults][italic_test_group]' => 'italic', - 'behavior_plugins[style][settings][defaults][regular_test_group]' => 'regular', - 'behavior_plugins[style][settings][defaults][underline_test_group]' => 'underline', + '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][regular_test_group]']); + $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][italic_test_group]']); + $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][underline_test_group]']); + $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]); @@ -453,38 +433,12 @@ // Change a plugin. $this->drupalGet('node/' . $node->id() . '/edit'); $edit = [ - 'paragraphs[0][behavior_plugins][style][style_wrapper][regular_test_group]' => 'bold' + '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'); - - // Configure no groups. - $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); - $edit = [ - 'behavior_plugins[style][settings][groups][]' => [], - ]; - $this->drupalPostAjaxForm(NULL, $edit, 'behavior_plugins[style][settings][groups][]'); - $edit = [ - 'behavior_plugins[style][settings][groups][]' => [], - ]; - $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][regular_test_group]']); - $this->assertEqual([], $styles); - $styles = $this->xpath('//select[contains(@name, :name)]', [':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][italic_test_group]']); - $this->assertEqual([], $styles); - $styles = $this->xpath('//select[contains(@name, :name)]', [':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][underline_test_group]']); - $this->assertEqual([], $styles); - $edit = [ - 'paragraphs[0][behavior_plugins][style][style_wrapper][style]' => 'underline' - ]; - $this->drupalPostForm(NULL, $edit, 'Save'); - $this->assertNoRaw('paragraphs-behavior-style--italic'); - $this->assertNoRaw('paragraphs-behavior-style--bold'); - $this->assertRaw('paragraphs-behavior-style--underline'); } } only in patch2: unchanged: --- 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' only in patch2: unchanged: --- /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' only in patch2: unchanged: --- 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' only in patch2: unchanged: --- /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' only in patch2: unchanged: --- a/paragraphs_collection.services.yml +++ b/paragraphs_collection.services.yml @@ -2,6 +2,9 @@ services: paragraphs_collection.style_discovery: class: \Drupal\paragraphs_collection\StyleDiscovery arguments: ['@module_handler', '@string_translation', '@controller_resolver', '@cache.discovery', '@theme_handler', '@config.factory'] + paragraphs_collection.style_group_discovery: + class: \Drupal\paragraphs_collection\StyleGroupDiscovery + arguments: ['@module_handler', '@cache.discovery', '@theme_handler', '@config.factory', '@paragraphs_collection.style_discovery'] paragraphs_collection.grid_layout_discovery: class: \Drupal\paragraphs_collection\GridLayoutDiscovery arguments: ['@module_handler', '@cache.discovery', '@theme_handler'] only in patch2: unchanged: --- /dev/null +++ b/src/StyleGroupDiscovery.php @@ -0,0 +1,129 @@ +moduleHandler = $module_handler; + $this->themeHandler = $theme_handler; + $this->cache = $cache_backend; + $this->configFactory = $config; + $this->stylesDiscovery = $style_discovery; + } + + /** + * {@inheritdoc} + */ + public function getStyleGroups() { + $cache_id = 'paragraphs_collection_style_group'; + if ($this->groupCollection !== NULL) { + return $this->groupCollection; + } + else if ($cached = $this->cache->get($cache_id)) { + $this->groupCollection = $cached->data; + } + else { + $yaml_discovery = $this->getYamlDiscovery(); + $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; + } + + /** + * {@inheritdoc} + */ + public function getGroupLabel($group_id) { + $groups = $this->getStyleGroups(); + if (in_array($group_id, array_keys($groups))) { + return $groups[$group_id]; + } + return NULL; + } + + /** + * Gets the YAML discovery. + * + * @return \Drupal\Core\Discovery\YamlDiscovery + * The YAML discovery. + */ + protected function getYamlDiscovery() { + return new YamlDiscovery('paragraphs.style_group', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories()); + } + +} only in patch2: unchanged: --- /dev/null +++ b/src/StyleGroupDiscoveryInterface.php @@ -0,0 +1,31 @@ +