Issue #3117291 by NitinLama, smustgrave, silverham, Chi: Element::isEmpty() should check for #weight property

merge-requests/2501/head
catch 2022-07-11 10:09:22 +09:00
parent 90f0da7baf
commit c3f6ce5cc0
2 changed files with 26 additions and 3 deletions

View File

@ -187,8 +187,8 @@ class Element {
/**
* Indicates whether the given element is empty.
*
* An element that only has #cache set is considered empty, because it will
* render to the empty string.
* An element that only has #cache, #weight, or #attached set is considered
* empty, because it will render to the empty string.
*
* @param array $elements
* The element.
@ -197,7 +197,7 @@ class Element {
* Whether the given element is empty.
*/
public static function isEmpty(array $elements) {
return empty($elements) || (count($elements) === 1 && array_keys($elements) === ['#cache']);
return \array_diff(\array_keys($elements), ['#cache', '#weight', '#attached']) === [];
}
}

View File

@ -188,6 +188,29 @@ class ElementTest extends UnitTestCase {
public function providerTestIsEmpty() {
return [
[[], TRUE],
[['#attached' => []], TRUE],
[['#cache' => []], TRUE],
[['#weight' => []], TRUE],
// Variations.
[['#attached' => [], '#cache' => []], TRUE],
[['#attached' => [], '#weight' => []], TRUE],
[['#attached' => [], '#weight' => [], '#cache' => []], TRUE],
[['#cache' => [], '#weight' => []], TRUE],
[['#cache' => [], '#weight' => [], '#any_other_property' => []], FALSE],
[
[
'#attached' => [],
'#weight' => [],
'#cache' => [],
'#any_other_property' => [],
],
FALSE,
],
// Cover sorting.
[['#cache' => [], '#weight' => [], '#attached' => []], TRUE],
[['#attached' => [], '#cache' => [], '#weight' => []], TRUE],
[['#weight' => [], '#attached' => [], '#cache' => []], TRUE],
[['#cache' => []], TRUE],
[['#cache' => ['tags' => ['foo']]], TRUE],
[['#cache' => ['contexts' => ['bar']]], TRUE],