Skip to content
Snippets Groups Projects

Optimize is empty rendering

Files
2
@@ -124,12 +124,12 @@ class ComponentElementAlter implements TrustedCallbackInterface {
* @return bool
* Returns true for empty.
*/
protected static function isSlotEmpty(array $slot, int $max_level = 1, int $level = 0): bool {
public static function isSlotEmpty(array $slot, int $max_level = 2, int $level = 0): bool {
if (is_array($slot) && empty($slot)) {
return TRUE;
}
if ($level < $max_level) {
foreach (Element::getVisibleChildren($slot) as $child) {
foreach (Element::children($slot) as $child) {
if (self::isSlotEmpty($slot[$child], $max_level, $level + 1) === FALSE) {
return FALSE;
}
@@ -138,7 +138,29 @@ class ComponentElementAlter implements TrustedCallbackInterface {
}
}
}
if (Element::isEmpty($slot)) {
return self::checkSlotEmpty($slot);
}
/**
* Advanced indicates whether the given element is empty.
*
* Before using Element::isEmpty($slot) the slot values are trimmed
* to catch more empty cases.
*
* @param array $slot
* The slot.
*
* @return bool
* Whether the given element is empty.
*/
private static function checkSlotEmpty(array $slot):bool {
foreach (['#markup', '#plain_text'] as $key) {
if (array_key_exists($key, $slot) && empty($slot[$key])) {
unset($slot[$key]);
}
}
if (Element::isEmpty($slot) || Element::isVisibleElement($slot) === FALSE) {
return TRUE;
}
return FALSE;
Loading