I have this rare case of a site where we have a node with an empty body, but the summary is not empty. When outputting the field on the full content view mode with the default format (i.e. the full body content, not the summary), Drupal does render the field wrappers, but an empty body:

<div class="field field-name-body field-type-text-with-summary field-label-hidden">
  <div class="field-items">
    <div property="content:encoded" class="field-item even"></div>
  </div>
</div>

In my case, this gives us unwanted markup, and given our CSS styling, unwanted style output.

Should the field view function not check for output, before actually rendering the field? In this case that would be: text_field_formatter_view(...)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Martijn Houtman’s picture

Martijn Houtman’s picture

Version: 7.3 » 7.x-dev
dcam’s picture

8.x needs to be checked for this issue. I can confirm it in 7.x, but due to a known problem with installing 8.x on Windows I'm currently unable to check in that version.

dcam’s picture

Version: 7.x-dev » 8.0.x-dev

I'm able to check D8 now and the issue does exist there.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kenorb’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 1: drupal-fields_do_not_render_when_empty-2309585-1.patch, failed testing.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

techwolf12’s picture

Still relevant

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Petr Illek’s picture

Title: HTML output for empty body field when summary is not empty » There should be no output of default formatter for empty text field when summary has content
Version: 8.5.x-dev » 8.7.x-dev
Status: Needs work » Needs review
FileSize
1.09 KB

Hi everyone,
recently we have got this issue in our project.

In the meantime Drupal changed the way how it is rendering the field value. So the original patch was not usable.

I've reworked it by adding a condition to the default TextDefaultFormatter.php in latest Drupal 8.7.x-dev.

kyberman’s picture

Status: Needs review » Reviewed & tested by the community

Hi @Petr,

thanks a lot for this patch. It definitely makes sense to skip output when there is no value.

Tested and works well. This should be easily merged soon.

Sam152’s picture

Status: Reviewed & tested by the community » Needs work

We should add a test for this. Should be easy enough to add a new testcase in \Drupal\Tests\text\Kernel\TextFormatterTest.

milos.kroulik’s picture

It seems that generated markup has changed:

    <div class="node__content clearfix">

        <div property="schema:text" data-quickedit-field-id="node/1/body/en/full" class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item quickedit-field"></div>

    </div>
milos.kroulik’s picture

@Sam152 I'm not sure about that class, because in the setUp() method there is only a field of type text created. Perhaps I should add a whole new class?

Also, I wonder if the Component should be changed to text.module, because it's implemented as part of it.

milos.kroulik’s picture

It seems that patch from #13 doesn't work anymore. Output from test is:

<div class="field field--name-name field--type-string field--label-hidden field__item">xBrt75S0</div>

I will have to try it agan.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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.

gease’s picture

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.

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.

JeroenT’s picture

Issue tags: +Bug Smash Initiative

Let's see if the patch still applies on Drupal 9.4

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.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.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

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs issue summary update

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Think this issue would benefit from an issue summary update. For clear steps to reproduce.

Reviewing the patch
1. It will need to be updated with D10 theme
2. Is this specific to summary? field (ties into issue summary update)

ameymudras’s picture

Status: Needs work » Needs review
FileSize
2.55 KB
822 bytes
smustgrave’s picture

Status: Needs review » Needs work

Still needs the issue summary updates.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.