diff -u b/config/install/paragraphs.paragraphs_type.container.yml b/config/install/paragraphs.paragraphs_type.container.yml --- b/config/install/paragraphs.paragraphs_type.container.yml +++ b/config/install/paragraphs.paragraphs_type.container.yml @@ -12,6 +11,0 @@ -behavior_plugins: - style: - enabled: true - groups: - general_group: - default: '' diff -u b/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style.yml b/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style.yml --- b/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style.yml +++ b/modules/paragraphs_collection_demo/paragraphs_collection_demo.paragraphs.style.yml @@ -3,26 +3,26 @@ 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 -u b/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style.yml b/modules/paragraphs_collection_test/paragraphs_collection_test.paragraphs.style.yml --- b/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 @@ title: 'Regular' description: 'Default style.' groups: - - 'regular_test_group' + - regular_test_group libraries: - 'paragraphs_collection_test/style' classes: @@ -14,22 +14,22 @@ 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' + - underline_test_group libraries: - 'paragraphs_collection_test/style' diff -u b/paragraphs_collection.install b/paragraphs_collection.install --- b/paragraphs_collection.install +++ b/paragraphs_collection.install @@ -20,15 +20,16 @@ * Update style plugin config of Paragraph types. */ function paragraphs_collection_update_8002() { - $paragraph_types = ParagraphsType::loadMultiple(); - foreach ($paragraph_types as $id => $paragraph_type) { + $config_factory = \Drupal::configFactory(); + foreach ($config_factory->listAll('paragraphs.paragraph_type.') as $name) { + $paragraph_type = $config_factory->getEditable($name); /** @var ParagraphsType $paragraph_type */ if ($paragraph_type->hasEnabledBehaviorPlugin('style')) { $default = ''; // Get the old configuration. if ($group = $paragraph_type->getBehaviorPlugin('style')->getConfiguration()['group']) { $default_style = $paragraph_type->getBehaviorPlugin('style')->getConfiguration()['default']; - $group_id = \Drupal::service('paragraphs_collection.style_group_discovery')->getGroupId($group); + $group_id = strtolower(str_replace(' ', '_', $group)); if ($style = \Drupal::service('paragraphs_collection.style_discovery')->getStyle($default_style)) { if (in_array($group_id, $style['groups'])) { $default = $default_style; reverted: --- b/paragraphs_collection.services.yml +++ a/paragraphs_collection.services.yml @@ -2,9 +2,6 @@ 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', '@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 @@ -13,7 +13,6 @@ use Drupal\paragraphs\ParagraphInterface; use Drupal\paragraphs\ParagraphsBehaviorBase; use Drupal\paragraphs_collection\StyleDiscoveryInterface; -use Drupal\paragraphs_collection\StyleGroupDiscoveryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -37,13 +36,6 @@ */ protected $yamlStyleDiscovery; - /** - * The yaml style group discovery. - * - * @var \Drupal\paragraphs_collection\StyleGroupDiscovery - */ - protected $yamlStyleGroupDiscovery; - /** * Constructs a new SelectionBase object. * @@ -57,13 +49,10 @@ * 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, StyleGroupDiscoveryInterface $style_group_discovery) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, StyleDiscoveryInterface $yaml) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager); $this->yamlStyleDiscovery = $yaml; - $this->yamlStyleGroupDiscovery = $style_group_discovery; } /** @@ -72,8 +61,7 @@ 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_group_discovery') + $container->get('paragraphs_collection.style_discovery') ); } @@ -100,7 +88,7 @@ //debug($this->yamlStyleDiscovery->getStyleOptions()); $form['style_wrapper']['styles'][$group_id] = [ '#type' => 'select', - '#title' => $this->t('%group Style', ['%group' => $this->yamlStyleGroupDiscovery->getGroupLabel($group_id)]), + '#title' => $this->t('%group Style', ['%group' => $this->yamlStyleDiscovery->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']], @@ -151,7 +139,7 @@ '#type' => 'select', '#title' => $this->t('Style groups'), '#multiple' => TRUE, - '#options' => $this->yamlStyleGroupDiscovery->getStyleGroups(), + '#options' => $this->yamlStyleDiscovery->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' => [ @@ -173,7 +161,7 @@ if (!empty($this->configuration['groups'][$group_id]['default'])) { $default = $this->configuration['groups'][$group_id]['default']; } - $group_label = $this->yamlStyleGroupDiscovery->getGroupLabel($group_id); + $group_label = $this->yamlStyleDiscovery->getGroupLabel($group_id); $form['groups_defaults'][$group_id]['default'] = [ '#type' => 'select', '#title' => $this->t($group_label . ' default style'), @@ -205,6 +193,9 @@ 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.')); + } } /** @@ -260,7 +251,7 @@ 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->yamlStyleGroupDiscovery->getGroupLabel($group_id), + '@group' => $this->yamlStyleDiscovery->getGroupLabel($group_id), '@style' => $style_options[$style] ]); } diff -u b/src/StyleDiscovery.php b/src/StyleDiscovery.php --- b/src/StyleDiscovery.php +++ b/src/StyleDiscovery.php @@ -163,41 +163,69 @@ } /** + * 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 -u b/src/StyleGroupDiscovery.php b/src/StyleDiscovery.php --- b/src/StyleGroupDiscovery.php +++ b/src/StyleDiscovery.php @@ -12,13 +12,21 @@ use Drupal\Core\StringTranslation\TranslationInterface; /** - * Provides common helper methods for style group discovery. + * Provides common helper methods for style discovery. + * @todo Create documentation for style discovery https://www.drupal.org/node/2837995 */ -class StyleGroupDiscovery implements StyleGroupDiscoveryInterface { +class StyleDiscovery implements StyleDiscoveryInterface { use StringTranslationTrait; /** + * Collection of styles with its definition. + * + * @var array + */ + protected $stylesCollection; + + /** * Collection of style groups with their definition. * * @var array @@ -88,6 +96,93 @@ /** * {@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; + } + else if ($cached = $this->cache->get($cache_id)) { + $this->stylesCollection = $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->stylesCollection[$style] = ['name' => $style]; + $this->stylesCollection[$style] += $definition + ['libraries' => []]; + } + } + $this->cache->set($cache_id, $this->stylesCollection); + } + return $this->stylesCollection; + } + + /** + * {@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]; + } + } + + return NULL; + } + + /** + * 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) { @@ -97,7 +192,7 @@ $this->groupCollection = $cached->data; } else { - $yaml_discovery = $this->getYamlDiscovery(); + $yaml_discovery = $this->getYamlGroupDiscovery(); $this->groupCollection = []; foreach ($yaml_discovery->findAll() as $module => $groups) { foreach ($groups as $group => $definition) { @@ -124,19 +219,12 @@ } /** - * {@inheritdoc} - */ - public function getGroupId($group_label) { - return strtolower(str_replace(' ', '_', $group_label)); - } - - /** - * Gets the YAML discovery. + * Gets the YAML group discovery. * * @return \Drupal\Core\Discovery\YamlDiscovery * The YAML discovery. */ - protected function getYamlDiscovery() { + protected function getYamlGroupDiscovery() { return new YamlDiscovery('paragraphs.style_group', $this->moduleHandler->getModuleDirectories() + $this->themeHandler->getThemeDirectories()); } reverted: --- b/src/StyleGroupDiscoveryInterface.php +++ /dev/null @@ -1,42 +0,0 @@ -