Skip to content

Commit 0f7c248

Browse files
author
Edouard Cunibil
committed
Prevent empty groups to be displayed.
1 parent c09132b commit 0f7c248

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ protected function preRenderGroup(array &$element, $group_name, array $rendering
126126
// Load field group settings.
127127
$group = $rendering_object['#fieldgroups'][$group_name];
128128

129+
/** @var \Drupal\Core\Entity\EntityInterface $entity */
130+
$entity = $rendering_object['#' . $rendering_object['#entity_type']];
131+
129132
// Handle groups managed by UI Patterns recursively.
130133
if ($group->format_type == 'pattern_formatter') {
134+
$element['#is_empty'] = TRUE;
135+
131136
// Move content into their fields.
132137
foreach ($group->format_settings['pattern_mapping'] as $field) {
133138
if ($field['plugin'] == 'fieldgroup') {
@@ -136,18 +141,49 @@ protected function preRenderGroup(array &$element, $group_name, array $rendering
136141
}
137142
else {
138143
$this->preRenderGroup($element[$field['source']], $field['source'], $rendering_object);
144+
if (!empty($element[$field['source']]) && !$element[$field['source']]['#is_empty']) {
145+
$element['#is_empty'] = FALSE;
146+
}
147+
}
148+
}
149+
elseif ($field['plugin'] == 'fields') {
150+
// Use entity field so the hypothetical rendering elements are not
151+
// to be taken into account to consider the field empty or not.
152+
if ($entity->hasField($field['source']) && !$entity->{$field['source']}->isEmpty()) {
153+
$element['#is_empty'] = FALSE;
139154
}
140155
}
156+
elseif (!empty($element[$field['source']])) {
157+
$element['#is_empty'] = FALSE;
158+
}
141159
$element['#fields'][$field['destination']][$field['source']] = $element[$field['source']];
142160
}
143161

144-
// Add render array metadata.
145-
$this->addRenderContext($element, $group->format_settings);
162+
// Prevent to show the group if fields are empty.
163+
if ($element['#is_empty'] && empty($group->format_settings['show_empty_fields'])) {
164+
$element = [];
165+
}
166+
else {
167+
// Add render array metadata.
168+
$this->addRenderContext($element, $group->format_settings);
169+
}
146170
}
147171
// Fallback to default pre_rendering for fieldgroups not managed by UI
148172
// Patterns.
149173
else {
150174
field_group_pre_render($element, $group, $rendering_object);
175+
$element['#is_empty'] = TRUE;
176+
foreach ($group->children as $child) {
177+
if (isset($rendering_object['#fieldgroups'][$child])) {
178+
$this->preRenderGroup($element[$child], $child, $rendering_object);
179+
if (!empty($element[$child]) && !$element[$child]['#is_empty']) {
180+
$element['#is_empty'] = FALSE;
181+
}
182+
}
183+
elseif ($entity->hasField($child) && !$entity->{$child}->isEmpty()) {
184+
$element['#is_empty'] = FALSE;
185+
}
186+
}
151187
}
152188
}
153189

0 commit comments

Comments
 (0)