From f17c8b2f2fb39aa6be86f891df138ce27798c866 Mon Sep 17 00:00:00 2001 From: Edouard Cunibil Date: Tue, 3 Dec 2019 15:40:42 +0100 Subject: [PATCH 1/4] Create an UiPatternsSource to be able to use field groups in patterns fields. --- .../UiPatterns/Source/FieldgroupSource.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 modules/ui_patterns_field_group/src/Plugin/UiPatterns/Source/FieldgroupSource.php diff --git a/modules/ui_patterns_field_group/src/Plugin/UiPatterns/Source/FieldgroupSource.php b/modules/ui_patterns_field_group/src/Plugin/UiPatterns/Source/FieldgroupSource.php new file mode 100644 index 00000000..e3694554 --- /dev/null +++ b/modules/ui_patterns_field_group/src/Plugin/UiPatterns/Source/FieldgroupSource.php @@ -0,0 +1,46 @@ +getContextProperty('entity_type'); + $bundle = $this->getContextProperty('entity_bundle'); + $view_mode = $this->getContextProperty('entity_view_mode'); + + $groups = field_group_info_groups($entity_type_id, $bundle, 'view', $view_mode); + + foreach ($groups as $group_name => $group) { + if (empty($this->getContextProperty('limit')) || in_array($group_name, $this->getContextProperty('limit'))) { + $sources[] = $this->getSourceField($group_name, $group->label); + } + } + + return $sources; + } + +} From 6756e77f2689e3c50a67e3f1379f69230bce008e Mon Sep 17 00:00:00 2001 From: Edouard Cunibil Date: Tue, 3 Dec 2019 17:43:53 +0100 Subject: [PATCH 2/4] Fix nested fieldgroups rendering. --- .../FieldGroupFormatter/PatternFormatter.php | 83 +++++++++++++++---- 1 file changed, 66 insertions(+), 17 deletions(-) diff --git a/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php b/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php index 20970b0c..caec4718 100644 --- a/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php +++ b/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php @@ -98,18 +98,70 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function preRender(&$element, $rendering_object) { + $this->preRenderGroup($element, $this->group->group_name, $rendering_object); + } - $fields = []; - $mapping = $this->getSetting('pattern_mapping'); - foreach ($mapping as $field) { - $fields[$field['destination']][] = $element[$field['source']]; + /** + * Recursive method to build the fieldgroup content. + * + * This method checks if one of the fieldgroup items are a fieldgroup pattern + * themselve. If so, we must build its configuration again and check if this + * fieldgroup doesn't have fieldgroup pattern items itself. (And the story + * keeps going until there are no more people alive on earth). + * + * @param array $element + * Renderable array of the outputed content. + * @param array $group_settings + * Pattern config settings. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + protected function preRenderGroup(array &$element, $group_name, array $rendering_object) { + // Do not pre render the group twice. + if (!empty($element['#pattern_pre_rendered'])) { + return; + } + + // Load field group settings. + $group = $rendering_object['#fieldgroups'][$group_name]; + + // Handle groups managed by UI Patterns recursively. + if ($group->format_type == 'pattern_formatter') { + // Move content into their fields. + foreach ($group->format_settings['pattern_mapping'] as $field) { + if ($field['plugin'] == 'fieldgroup') { + $this->preRenderGroup($element[$field['source']], $field['source'], $rendering_object); + } + $element['#fields'][$field['destination']][$field['source']] = $element[$field['source']]; + } + + // Add render array metadata. + $this->addRenderContext($element, $group->format_settings); + } + // Fallback to default pre_rendering for fieldgroups not managed by UI + // Patterns. + else { + field_group_pre_render($element, $group, $rendering_object); + } + } + + /** + * Helper to build the context expected to render the fieldgroup pattern. + * + * @param array $element + * Field data. + * @param array $format_settings + * The pattern format settings. + */ + protected function addRenderContext(array &$element, array $format_settings) { + $element['#id'] = $format_settings['pattern']; + if (!empty($format_settings['pattern_variant'])) { + $element['#variant'] = $format_settings['pattern_variant']; } $element['#type'] = 'pattern'; - $element['#id'] = $this->getSetting('pattern'); - $element['#fields'] = $fields; $element['#multiple_sources'] = TRUE; - $element['#variant'] = $this->getSetting('pattern_variant'); // Allow default context values to not override those exposed elsewhere. $element['#context']['type'] = 'field_group'; @@ -119,17 +171,13 @@ public function preRender(&$element, $rendering_object) { $element['#context']['view_mode'] = $this->configuration['group']->mode; // Pass current entity to pattern context, if any. - $element['#context']['entity'] = $this->entityFinder->findEntityFromFields($element['#fields']); - } + if (!empty($element['#fields'])) { + $element['#context']['entity'] = $this->entityFinder->findEntityFromFields($element['#fields']); + } - /** - * Get field group name. - * - * @return string - * Field group name. - */ - protected function getFieldGroupName() { - return $this->configuration['group']->group_name; + // Nested groups can be rendered in any order so mark this one as done to + // prevent issues. + $element['#pattern_pre_rendered'] = TRUE; } /** @@ -144,6 +192,7 @@ public function settingsForm() { $context = [ 'entity_type' => $this->configuration['group']->entity_type, 'entity_bundle' => $this->configuration['group']->bundle, + 'entity_view_mode' => $this->configuration['group']->mode, 'limit' => $this->configuration['group']->children, ]; From c09132b8f615ad05cccc201597f74c3801e9c6d8 Mon Sep 17 00:00:00 2001 From: Edouard Cunibil Date: Tue, 23 Nov 2021 15:21:14 +0100 Subject: [PATCH 3/4] Allow to use field_group label in patterns. --- .../src/Plugin/UiPatterns/Source/FieldgroupSource.php | 1 + .../field_group/FieldGroupFormatter/PatternFormatter.php | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/ui_patterns_field_group/src/Plugin/UiPatterns/Source/FieldgroupSource.php b/modules/ui_patterns_field_group/src/Plugin/UiPatterns/Source/FieldgroupSource.php index e3694554..b7d557a4 100644 --- a/modules/ui_patterns_field_group/src/Plugin/UiPatterns/Source/FieldgroupSource.php +++ b/modules/ui_patterns_field_group/src/Plugin/UiPatterns/Source/FieldgroupSource.php @@ -39,6 +39,7 @@ public function getSourceFields() { $sources[] = $this->getSourceField($group_name, $group->label); } } + $sources[] = $this->getSourceField('_label', 'Group label'); return $sources; } diff --git a/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php b/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php index caec4718..05dc508c 100644 --- a/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php +++ b/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php @@ -131,7 +131,12 @@ protected function preRenderGroup(array &$element, $group_name, array $rendering // Move content into their fields. foreach ($group->format_settings['pattern_mapping'] as $field) { if ($field['plugin'] == 'fieldgroup') { - $this->preRenderGroup($element[$field['source']], $field['source'], $rendering_object); + if ($field['source'] === '_label') { + $element[$field['source']] = ['#markup' => $group->label]; + } + else { + $this->preRenderGroup($element[$field['source']], $field['source'], $rendering_object); + } } $element['#fields'][$field['destination']][$field['source']] = $element[$field['source']]; } From 0f7c248630ffa5196092853b66ddadcdef6cc36b Mon Sep 17 00:00:00 2001 From: Edouard Cunibil Date: Tue, 30 Nov 2021 11:06:35 +0100 Subject: [PATCH 4/4] Prevent empty groups to be displayed. --- .../FieldGroupFormatter/PatternFormatter.php | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php b/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php index 05dc508c..db1b42f7 100644 --- a/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php +++ b/modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php @@ -126,8 +126,13 @@ protected function preRenderGroup(array &$element, $group_name, array $rendering // Load field group settings. $group = $rendering_object['#fieldgroups'][$group_name]; + /** @var \Drupal\Core\Entity\EntityInterface $entity */ + $entity = $rendering_object['#' . $rendering_object['#entity_type']]; + // Handle groups managed by UI Patterns recursively. if ($group->format_type == 'pattern_formatter') { + $element['#is_empty'] = TRUE; + // Move content into their fields. foreach ($group->format_settings['pattern_mapping'] as $field) { if ($field['plugin'] == 'fieldgroup') { @@ -136,18 +141,49 @@ protected function preRenderGroup(array &$element, $group_name, array $rendering } else { $this->preRenderGroup($element[$field['source']], $field['source'], $rendering_object); + if (!empty($element[$field['source']]) && !$element[$field['source']]['#is_empty']) { + $element['#is_empty'] = FALSE; + } + } + } + elseif ($field['plugin'] == 'fields') { + // Use entity field so the hypothetical rendering elements are not + // to be taken into account to consider the field empty or not. + if ($entity->hasField($field['source']) && !$entity->{$field['source']}->isEmpty()) { + $element['#is_empty'] = FALSE; } } + elseif (!empty($element[$field['source']])) { + $element['#is_empty'] = FALSE; + } $element['#fields'][$field['destination']][$field['source']] = $element[$field['source']]; } - // Add render array metadata. - $this->addRenderContext($element, $group->format_settings); + // Prevent to show the group if fields are empty. + if ($element['#is_empty'] && empty($group->format_settings['show_empty_fields'])) { + $element = []; + } + else { + // Add render array metadata. + $this->addRenderContext($element, $group->format_settings); + } } // Fallback to default pre_rendering for fieldgroups not managed by UI // Patterns. else { field_group_pre_render($element, $group, $rendering_object); + $element['#is_empty'] = TRUE; + foreach ($group->children as $child) { + if (isset($rendering_object['#fieldgroups'][$child])) { + $this->preRenderGroup($element[$child], $child, $rendering_object); + if (!empty($element[$child]) && !$element[$child]['#is_empty']) { + $element['#is_empty'] = FALSE; + } + } + elseif ($entity->hasField($child) && !$entity->{$child}->isEmpty()) { + $element['#is_empty'] = FALSE; + } + } } }