@@ -126,8 +126,13 @@ protected function preRenderGroup(array &$element, $group_name, array $rendering
126
126
// Load field group settings.
127
127
$ group = $ rendering_object ['#fieldgroups ' ][$ group_name ];
128
128
129
+ /** @var \Drupal\Core\Entity\EntityInterface $entity */
130
+ $ entity = $ rendering_object ['# ' . $ rendering_object ['#entity_type ' ]];
131
+
129
132
// Handle groups managed by UI Patterns recursively.
130
133
if ($ group ->format_type == 'pattern_formatter ' ) {
134
+ $ element ['#is_empty ' ] = TRUE ;
135
+
131
136
// Move content into their fields.
132
137
foreach ($ group ->format_settings ['pattern_mapping ' ] as $ field ) {
133
138
if ($ field ['plugin ' ] == 'fieldgroup ' ) {
@@ -136,18 +141,49 @@ protected function preRenderGroup(array &$element, $group_name, array $rendering
136
141
}
137
142
else {
138
143
$ 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 ;
139
154
}
140
155
}
156
+ elseif (!empty ($ element [$ field ['source ' ]])) {
157
+ $ element ['#is_empty ' ] = FALSE ;
158
+ }
141
159
$ element ['#fields ' ][$ field ['destination ' ]][$ field ['source ' ]] = $ element [$ field ['source ' ]];
142
160
}
143
161
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
+ }
146
170
}
147
171
// Fallback to default pre_rendering for fieldgroups not managed by UI
148
172
// Patterns.
149
173
else {
150
174
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
+ }
151
187
}
152
188
}
153
189
0 commit comments