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..07c3c25a 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 @@ -128,6 +128,8 @@ protected function preRenderGroup(array &$element, $group_name, array $rendering // 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,13 +138,35 @@ 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') { + /** @var \Drupal\Core\Entity\EntityInterface $entity */ + $entity = $rendering_object['#' . $rendering_object['#entity_type']]; + + // 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.