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.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

prudloff created an issue. See original summary.

arora.shivani made their first commit to this issue’s fork.

pcambra’s picture

Version: 8.x-2.x-dev » 3.0.x-dev

prudloff changed the visibility of the branch 3443679-use-elementisempty-to to hidden.

pcambra’s picture

  • pcambra committed 367be824 on 3.0.x authored by prudloff
    Issue #3443679: Use Element::isEmpty() to check if a field is empty
    
pcambra’s picture

Status: Active » Fixed

Thanks!

pcambra’s picture

Status: Fixed » Closed (fixed)

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