Skip to content

Commit

Permalink
Prevent empty groups to be displayed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard Cunibil committed Nov 30, 2021
1 parent c09132b commit 3bb7917
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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.
Expand Down

0 comments on commit 3bb7917

Please sign in to comment.