diff -u b/modules/paragraphs_collection_demo/paragraphs_collection_demo.install b/modules/paragraphs_collection_demo/paragraphs_collection_demo.install --- b/modules/paragraphs_collection_demo/paragraphs_collection_demo.install +++ b/modules/paragraphs_collection_demo/paragraphs_collection_demo.install @@ -82,11 +82,10 @@ ], 'style' => [ 'enabled' => TRUE, - 'groups' => ['general_group'], + 'groups' => ['general_group' => ['default' => '']], ], ]); $container->save(); - } /** @@ -108,7 +107,7 @@ '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 -u b/paragraphs_collection.services.yml b/paragraphs_collection.services.yml --- b/paragraphs_collection.services.yml +++ b/paragraphs_collection.services.yml @@ -4,7 +4,7 @@ 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'] + arguments: ['@module_handler', '@string_translation', '@controller_resolver', '@cache.discovery', '@theme_handler', '@config.factory'] paragraphs_collection.grid_layout_discovery: class: \Drupal\paragraphs_collection\GridLayoutDiscovery arguments: ['@module_handler', '@cache.discovery', '@theme_handler'] 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 @@ -97,6 +97,7 @@ if (isset($this->configuration['groups'][$group_id]['default'])) { $plugin_default = $this->configuration['groups'][$group_id]['default']; } + //debug($this->yamlStyleDiscovery->getStyleOptions()); $form['style_wrapper']['styles'][$group_id] = [ '#type' => 'select', '#title' => $this->t('%group Style', ['%group' => $this->yamlStyleGroupDiscovery->getGroupLabel($group_id)]), @@ -149,7 +150,6 @@ $form['groups'] = [ '#type' => 'select', '#title' => $this->t('Style groups'), - '#required' => TRUE, '#multiple' => TRUE, '#options' => $this->yamlStyleGroupDiscovery->getStyleGroups(), '#description' => $this->t('Restrict available styles to a certain style group. Select none to allow all styles.'), @@ -161,8 +161,7 @@ ]; // @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']); + $groups = $form_state->getCompleteFormState()->getValue($group_key, $this->configuration['groups']); $form['groups_defaults'] = [ '#type' => 'container', '#prefix' => '
', reverted: --- b/src/StyleGroupDiscovery.php +++ /dev/null @@ -1,136 +0,0 @@ -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; - } - - /** - * {@inheritdoc} - */ - public function getGroupId($group_label) { - return strtolower(str_replace(' ', '_', $group_label)); - } - - /** - * 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()); - } - -} diff -u b/src/Tests/ParagraphsStylePluginTest.php b/src/Tests/ParagraphsStylePluginTest.php --- b/src/Tests/ParagraphsStylePluginTest.php +++ b/src/Tests/ParagraphsStylePluginTest.php @@ -283,12 +283,12 @@ // 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(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'); @@ -297,6 +297,11 @@ '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. @@ -305,27 +310,22 @@ 'styles[italic][enabled]' => TRUE, ]; $this->drupalPostForm(NULL, $edit, 'Save configuration'); - /* $node = $this->getNodeByTitle('global_settings'); + $node = $this->getNodeByTitle('global_settings'); $this->drupalGet('node/' . $node->id()); // Assert that the class of the plugin is not added if disabled. $this->assertNoRaw('paragraphs-behavior-style--bold'); $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], 'Italic'); - // Check that styles are not shown in style config when disabled. - $edit = [ - 'behavior_plugins[style][settings][groups][]' => 'Italic Test Group' - ]; - $this->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, 'Save'); $this->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type); - $options = $this->xpath('//*[contains(@name,"behavior_plugins[style][settings][defaults][italic_test_group]")]/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');*/ + $this->assertEqual($options[1], 'Italic'); } /** only in patch2: unchanged: --- a/config/install/paragraphs.paragraphs_type.container.yml +++ b/config/install/paragraphs.paragraphs_type.container.yml @@ -12,3 +12,6 @@ description: 'Use Container to group paragraphs to apply a common style behavior_plugins: style: enabled: true + groups: + general_group: + default: '' only in patch2: unchanged: --- a/paragraphs_collection.module +++ b/paragraphs_collection.module @@ -68,7 +68,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']); } /** only in patch2: unchanged: --- 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']; } } only in patch2: unchanged: --- a/src/StyleDiscovery.php +++ b/src/StyleGroupDiscovery.php @@ -12,21 +12,13 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; /** - * Provides common helper methods for style discovery. - * @todo Create documentation for style discovery https://www.drupal.org/node/2837995 + * Provides common helper methods for style group discovery. */ -class StyleDiscovery implements StyleDiscoveryInterface { +class StyleGroupDiscovery implements StyleGroupDiscoveryInterface { use StringTranslationTrait; /** - * Collection of styles with its definition. - * - * @var array - */ - protected $stylesCollection; - - /** * Collection of style groups with their definition. * * @var array @@ -96,90 +88,46 @@ class StyleDiscovery implements StyleDiscoveryInterface { /** * {@inheritdoc} */ - public function getStyleOptions($group = '') { - $options = []; - $style_collection = $this->getStyles(); - $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']))) { - $options[$style] = $definition['title']; - } - } - }; - uasort($options, 'strcasecmp'); - return $options; - } - - /** - * {@inheritdoc} - */ - public function getStyles() { - $cache_id = 'paragraphs_collection_style'; - if ($this->stylesCollection !== NULL) { - return $this->stylesCollection; + 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->stylesCollection = $cached->data; + $this->groupCollection = $cached->data; } else { $yaml_discovery = $this->getYamlDiscovery(); - $this->stylesCollection = []; - foreach ($yaml_discovery->findAll() as $module => $styles) { - foreach ($styles as $style => $definition) { - if (empty($definition['title'])) { - throw new InvalidStyleException('The "title" of "' . $style . '" must be non-empty.'); - } - $definition['title'] = $this->t($definition['title']); - if (!empty($definition['description'])) { - $definition['description'] = $this->t($definition['description']); + $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->stylesCollection[$style] = ['name' => $style]; - $this->stylesCollection[$style] += $definition + ['libraries' => []]; + $this->groupCollection[$group] = $this->t($definition['label']); } } - $this->cache->set($cache_id, $this->stylesCollection); + $this->cache->set($cache_id, $this->groupCollection); } - return $this->stylesCollection; + return $this->groupCollection; } /** * {@inheritdoc} */ - public function getStyle($style, $default = NULL) { - $styles = $this->getStyles(); - $enabled_styles = $this->configFactory->get('paragraphs_collection.settings')->get('enabled_styles'); - if (empty($enabled_styles) || in_array($style, $enabled_styles)) { - if (isset($styles[$style])) { - return $styles[$style]; - } - - if ($default && isset($styles[$default])) { - return $styles[$default]; - } + public function getGroupLabel($group_id) { + $groups = $this->getStyleGroups(); + if (in_array($group_id, array_keys($groups))) { + return $groups[$group_id]; } - return NULL; } /** * {@inheritdoc} */ - public function getStyleGroups() { - 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 { - $this->groupCollection = []; - } - return $this->groupCollection; + public function getGroupId($group_label) { + return strtolower(str_replace(' ', '_', $group_label)); } /** @@ -189,15 +137,7 @@ class StyleDiscovery implements StyleDiscoveryInterface { * 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']; + return new YamlDiscovery('paragraphs.style_group', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories()); } }