Currently the element is considered empty if it only has #cache property. The #weight property should be considered too as it does not change the emptiness of an element.

Comments

Chi created an issue. See original summary.

silverham’s picture

agree.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

silverham’s picture

And #attached property too.

@see \Drupal\Core\Render\BubbleableMetadata::applyTo()

NitinLama’s picture

Assigned: Unassigned » NitinLama
Status: Active » Needs review
StatusFileSize
new1.03 KB

Let me know if this is fine.

Status: Needs review » Needs work

The last submitted patch, 5: weight_property_check-3117291-5.patch, failed testing. View results

NitinLama’s picture

Status: Needs work » Needs review
StatusFileSize
new1.25 KB
new1.25 KB

Let's see.

NitinLama’s picture

StatusFileSize
new1.25 KB

The last submitted patch, 7: weight_property_check-3117291-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 7: interdiff_5_7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

silverham’s picture

StatusFileSize
new2.85 KB

Pervious patch only works is there is only 1 element.

Attached patch for 1, 2 or 3 number of elements.

silverham’s picture

Status: Needs work » Needs review
tanubansal’s picture

Tested #12 on 9.1, its working for more than 1 element

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

StatusFileSize
new2.85 KB
new481 bytes

Just a spelling fix

chi’s picture

Status: Needs review » Needs work

public static function isEmpty(array $elements) {
- return empty($elements) || (count($elements) === 1 && array_keys($elements) === ['#cache']);
+ // Early check.
+ if (empty($elements)) {
+ return TRUE;
+ }
+ $is_empty = FALSE;
+ $element_keys = array_keys($elements);
+ // Sort so we know the which elements are where.
+ sort($element_keys);
+ $count = count($element_keys);
+ // Use strict comparison.
+ switch (TRUE) {
+ case ($count === 1):
+ // Sort by most common for performance.
+ if (($element_keys === ['#cache'])
+ || ($element_keys === ['#weight'])
+ || ($element_keys === ['#attached'])) {
+ $is_empty = TRUE;
+ }
+
+ case ($count === 2):
+ if (($element_keys === ['#cache', '#weight'])
+ || ($element_keys === ['#attached', '#weight'])
+ || ($element_keys === ['#attached', '#cache'])) {
+ $is_empty = TRUE;
+ }
+
+ case ($count === 3):
+ if ($element_keys === ['#attached', '#cache', '#weight']) {
+ $is_empty = TRUE;
+ }
+
+ }
+ return $is_empty;
}

There are simpler ways to check this.

return \array_diff(\array_keys($elements), ['#cache', '#weight', '#attached']) === [];
unset($elements['#cache'], $elements['#weight'], $elements['#attached']);
return $elements === [];

Also method documentation needs to be updated.

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new2.42 KB
new1.75 KB

@chi thanks!

chi’s picture

Status: Needs review » Reviewed & tested by the community

Thank you.

  • catch committed 759f951 on 10.0.x
    Issue #3117291 by NitinLama, smustgrave, silverham, Chi: Element::...
  • catch committed c3f6ce5 on 10.1.x
    Issue #3117291 by NitinLama, smustgrave, silverham, Chi: Element::...
  • catch committed 623dfc9 on 9.5.x
    Issue #3117291 by NitinLama, smustgrave, silverham, Chi: Element::...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.1.x, cherry-picked to 10.0.x and 9.5.x, thanks! Not backporting to 9.4.x just in case there's something relying on the current behaviour somewhere.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

silverham’s picture

Thanks Team!

Re There are simpler ways to check this.

Wasn't sure which was the most performant way to do it. But it's only a single array, so maybe performance difference is too small to matter.

miiimooo’s picture

@catch This change does more than it says in the issue title: it also removes output from block plugins that return the #attached property.

Is this intended?

For me this breaks blocks that only attach drupalSettings for instance. Sure this must be affecting other sites

I've opened #3333858: Blocks that have #attached set removed after upgrade and added a patch to revert this behaviour

larowlan’s picture

Yes, this also caused https://drupal.org/project/nsw_feedback to stop working

chi’s picture

StatusFileSize
new2.79 KB

Sounds like elements with #attached property shouldn't be considered as empty.

miiimooo’s picture

chi’s picture

@miiimooo Ok, I think it's better to continue work in #3333858: Blocks that have #attached set removed after upgrade.