diff --git a/core/lib/Drupal/Core/Render/Element.php b/core/lib/Drupal/Core/Render/Element.php index 75c3ab74017..dbd0804119a 100644 --- a/core/lib/Drupal/Core/Render/Element.php +++ b/core/lib/Drupal/Core/Render/Element.php @@ -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']) === []; } } diff --git a/core/tests/Drupal/Tests/Core/Render/ElementTest.php b/core/tests/Drupal/Tests/Core/Render/ElementTest.php index 1c795c7dba0..b847e672af2 100644 --- a/core/tests/Drupal/Tests/Core/Render/ElementTest.php +++ b/core/tests/Drupal/Tests/Core/Render/ElementTest.php @@ -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],