Skip to content
Snippets Groups Projects
Commit 078c6619 authored by christian.wiedemann's avatar christian.wiedemann
Browse files

Merge branch '3492962-2.0.0-rc1-empty-slot' into '2.0.x'

Optimize is empty rendering

See merge request !297
parents 5cab2636 02b4f057
No related branches found
No related tags found
No related merge requests found
Pipeline #365196 canceled
......@@ -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;
......
<?php
declare(strict_types=1);
namespace Drupal\Tests\ui_patterns\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\ui_patterns\Element\ComponentElementAlter;
/**
* @coversDefaultClass \Drupal\ui_patterns\Element\ComponentElementAlter
*
* @group ui_patterns
*/
final class ComponentElementAlterTest extends UnitTestCase {
/**
* Test the method ::canonicalize().
*
* @dataProvider provideSlots
*/
public function testIsSlotEmpty(array $slot, bool $isEmpty): void {
$this->assertEquals($isEmpty, ComponentElementAlter::isSlotEmpty($slot));
}
/**
* Provide data for testCanonicalize.
*/
public static function provideSlots(): array {
return [
[
['#markup' => ''], TRUE,
],
[
['#cache' => ['tags' => ['tag1']]], TRUE,
],
[
['#cache' => ['tags' => ['tag2']], '#markup' => NULL], TRUE,
],
[
['#cache' => ['tags' => ['tag2']], 'children' => [['#markup' => '']]], TRUE,
],
[
['#plain_text' => ''], TRUE,
],
[
['#plain_text' => '', '#preprocess' => 'dummy'], FALSE,
],
[
[
'children' => [
['#markup' => ''],
['#markup' => ''],
],
], TRUE,
],
[
[
'children' => [
['#markup' => ''],
['#markup' => 'TEST'],
],
], FALSE,
],
[
['children' => ['#markup' => 'some']], FALSE,
],
[
['#markup' => 'some data'], FALSE,
],
[
['#theme' => 'my_theme'], FALSE,
],
[
['#weight' => 0], TRUE,
],
[
['#theme' => 'my_theme', '#access' => FALSE], TRUE,
],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment