Use Element::isEmpty() to check if a field is empty

Created on 26 April 2024, 7 months ago

Problem/Motivation

ExtraFieldDisplayFormattedBase::view() checks if a field is empty with !empty($elements).
I think it would make more sense to use Element::isEmpty() here, as it would consider a field with only #cache empty.

I know we can manually specify if a field is empty with something like this:

    $this->isEmpty = Element::isEmpty($build));

But it would be useful if we did not have to specify it for basic use cases. (Specifying isEmpty manually would still be useful for more complex cases.)

Steps to reproduce

Very basic example of a field that is empty but does not return an empty array:

  public function viewElements(ContentEntityInterface $entity): array {
    return [
      '#cache' => ['max-age' => 42],
    ];
  }

Proposed resolution

We could do something like this:

    if (!Element::isEmpty($elements) && !$this->isEmpty()) {

But would this break retro-compatibility? I guess it could consider fields empty that were not considered empty before.

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇫🇷France prudloff Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024