Commit 759f9516 authored by catch's avatar catch
Browse files

Issue #3117291 by NitinLama, smustgrave, silverham, Chi: Element::isEmpty()...

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

(cherry picked from commit c3f6ce5c)
parent 45663e48
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -187,8 +187,8 @@ public static function setAttributes(array &$element, array $map) {
  /**
   * 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 @@ public static function setAttributes(array &$element, array $map) {
   *   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']) === [];
  }

}
+23 −0
Original line number Diff line number Diff line
@@ -188,6 +188,29 @@ public function testIsEmpty(array $element, $expected) {
  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],