Context

  • Using a view with table formatter, hide empty cells enabled
  • twig.config.debug = true in services.yml

Problem

The empty cells are all being output due to the added twig comments

Proposed solution

Filter the cells prior to the comments being added
AND / OR make it possible to enable twig debug dynamically by a parameter so that it doesn't have to be on in the dev environment on every page load #2947316: How to alter or set twig.config values in PHP conditionally / dynamically?

Issue fork drupal-2950758

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

Anybody created an issue. See original summary.

anybody’s picture

Title: Empty table cells not hidden if twig debug is true » Empty table cells never hidden if twig debug is true
DragoonBoots’s picture

StatusFileSize
new1.07 KB

I was having a hard time trying to figure out why the option to hide empty columns wasn't working and found this issue. Here's a quick and dirty patch that fixes this problem for me.

DragoonBoots’s picture

Status: Active » Needs review

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mattew’s picture

StatusFileSize
new1.16 KB

Add a test to check if the field has been excluded.

anybody’s picture

Status: Needs review » Needs work

Well I think the patch from #3 is way too dirty for production. It even runs if twig debug is false and costs too much performance.
It would be okay if the case only runs if twig debug is true.

mattew’s picture

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

Here is an updated patch which contains the test against debug mode status.

anybody’s picture

Well that looks good from my point of view. I haven't tested it yet but it's an important step forward. Further tests and feedback?

mattew’s picture

I'm not quite sure if the regex is totally bullet-proof to exclude HTML comments. Maybe somebody could check this?

mattew’s picture

Oh, maybe we could implement a dedicated test for that case ? Something like \Drupal\Tests\views\Functional\Plugin\StyleTableTest::testEmptyColumn but with the twig debug enabled.

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.

ivnish’s picture

It's not only views tables problem. I use fields in views. I set "hide field if empty", but it doesn't hide if twig debug is on

emb03’s picture

Yes, I can confirm this is a bug in views-view-fields as well in support of #13

r0nn1ef’s picture

This also applies if the title for the display is not empty on Drupal 8.7.7.

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.

jopdebeeck’s picture

StatusFileSize
new2.42 KB

I added the twig_debug check and stripping and trimming of the field if it's enabled to the isValueEmpty check in FieldPluginBase.php.
This should fix the "hide field if empty" problems for fields in views when twig_debug is enabled. (#13 & #14)

jopdebeeck’s picture

StatusFileSize
new2.37 KB

I remade the patch because it failed to apply.

Status: Needs review » Needs work

The last submitted patch, 18: 2950758-views-hide-empty-18.patch, failed testing. View results

lendude’s picture

Category: Bug report » Feature request

This to me is not a bug, it's not empty => hence it isn't hidden.

But yeah I've run into this too, but still....

Adding a workaround for twig debug changing our definition of empty sounds like a feature (and not one I'm in favour off, turn off twig debug if you want to see what it looks like on production, we don't need more regexes and complexity in the views rendering pipeline, we have plenty of that already). Just my 2ct obviously.

ruslan piskarov’s picture

Unfortunately #18 strip also the comment like this "<!--form-item-entity_browser_select--paragraphs_library_item:1-->" and in the result checkboxes not visible:
Without patch: https://take.ms/iDQOU
With patch: https://take.ms/i9g8N

I found this issue with the "Paragraph Library" module.

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.

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.

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.

dpi’s picture

$value = preg_replace('/<!--.*?-->/s', '', $value);

This seems to break Media Library pretty badly. The checkboxes disappear on each item.

The regex captures and delete the placeholder <!-- -->, e.g <!--form-item-media_library_select_form--0-->. See \Drupal\media_library\Plugin\views\field\MediaLibrarySelectForm::getValue

jcmartinez made their first commit to this issue’s fork.

jcmartinez’s picture

StatusFileSize
new2.5 KB

I just made a small commit that solves for me the issue mentioned in #28.

I also made a merge request.

You can download the patch as a diff directly from my commit. I'm also attaching a patch file here.

_utsavsharma’s picture

StatusFileSize
new729 bytes
new2.38 KB

Tried to fix CCF for #31.

jwilson3’s picture

dpi’s picture

32 incorrectly introduces a space in the regex.

dpi’s picture

Noticed a problem with the patch. The following error is raised, likely due to PHP8 changes.

Deprecated function: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in Drupal\views\Plugin\views\field\FieldPluginBase->isValueEmpty() (line 1226 of core/modules/views/src/Plugin/views/field/FieldPluginBase.php).

We should ensure we don't enter the if (\Drupal::service('twig')->isDebug()) { condition if value is NULL.

tanuj.’s picture

Status: Needs work » Needs review
StatusFileSize
new8.66 KB
new7.36 KB

Adding a new patch addressing points from #34 and #35, and also fixing some phpcs errors. please review.

Status: Needs review » Needs work

The last submitted patch, 36: 2950758-36.patch, failed testing. View results

dpi’s picture

  1. +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
    @@ -1236,6 +1240,15 @@ public function isValueEmpty($value, $empty_zero, $no_skip_empty = TRUE) {
    +    if ($twig_debug_on == TRUE && !is_null($twig_debug_on)) {
    

    the previous way of doing it, from 32 and before, was fine. no need to assign and check TRUE

    this is also checking the wrong value for whether it is NULL. you should be checking $value. Not the result of isDebug()

    $value !== NULL will be fine for the null check.

  2. +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
    @@ -1236,6 +1240,15 @@ public function isValueEmpty($value, $empty_zero, $no_skip_empty = TRUE) {
    +      $value = preg_replace('/<!--.*?-->/s', '', (string) $value);
    

    this correctly removed the leading space

@TanujJain-TJ there are many changes in the new patch compared to the previous without explanation, in addition to an overly large interdiff? Have you brought in irrelevant changes?

Tests continue to fail.

NW

tanuj.’s picture

Status: Needs work » Needs review
StatusFileSize
new7.02 KB
new5.94 KB

@dpi sorry for that null check i interpreted it wrong at first updating the patch to correct all the changes required, also these out of scope changes are just PHPCS fixes i got while running this below given command, did it so the patch doesn't fail Custom commands check. adding new interdiff with #32.
phpcs --standard="Drupal,DrupalPractice" --extensions="php,module,inc,install,test,profile,theme,css,info,txt,md,yml"

dpi’s picture

Status: Needs review » Needs work

Please address the CS changes relevant to the lines being modified only.

tanuj.’s picture

Status: Needs work » Needs review
StatusFileSize
new2.43 KB
new2.05 KB

updated according to #40, please review.

Status: Needs review » Needs work

The last submitted patch, 41: 2950758-41.patch, failed testing. View results

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.

vasike’s picture

@Tanuj ... for tests to pass i think you also need to check the twig service exists

So, maybe you could update patch (or create new MR) with something like:

    // If twig_debug is enabled we strip out the
    // html comments before running the empty check.
    if ($value !== NULL) {
      if (\Drupal::hasService('twig') && \Drupal::service('twig')->isDebug()) {
        $value = preg_replace('/<!--.*?-->/s', '', (string) $value);
        $value = trim($value);
      }
    }
demonde’s picture

@Tanuj: Patch #41 works, but the core media library form widget does not work anymore. You cannot select items there.

uddeshy2’s picture

@Tanuj: I can confirm that after applying patch #41, the core media library widget (both grid and table views) is not functioning correctly. The checkboxes are not being rendered when twig debugging is enabled.

tgoeg’s picture

I am not sure whether this solves all the problems people face here, but I have a completely different approach that solves it for me with views at least and does not introduce any additional logic.

When I check for empty fields, the problem is not the added XML-commented debug output per se, but the added whitespace/line breaks that are not inside the comments and therefore lead to twig believing there is content indeed.

I first thought that removing the line breaks would mess up the generated markup (but I wanted to live with it, as long as it would solve the initial problem of seeing different rendered things with and without debug mode enabled).
But as it turns out, both firefox and chromium take care of adding linebreaks themselves when using developer tools in the browser and the markup looks just the same as with the patch. And it makes checks in twig whether fields are empty truely true :-)

I am not a developer so please improve as you see fit. Just posting here as it fixes it for me and seems less intrusive.
Applies against 10.1.x and 10.2.x.

uddeshy2’s picture

StatusFileSize
new2.84 KB
new3.55 KB

I have created a new patch over #41 after observing the patch issues in #46. This patch fixes the issue of checkboxes not showing in media library picker when twig debug is on.

kristofferwiklund’s picture

Does not apply to Drupal 11.3.2

kristofferwiklund changed the visibility of the branch 2950758-empty-table-cells to hidden.

kristofferwiklund changed the visibility of the branch 2950758-empty-table-cells to active.

kristofferwiklund changed the visibility of the branch 2950758-empty-table-cells to active.

liam morland made their first commit to this issue’s fork.

kristofferwiklund’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

kristofferwiklund’s picture

kristofferwiklund changed the visibility of the branch 2950758-empty-table-cells-d11 to hidden.

kadamsubodh0619’s picture

Hello ,
I am using Drupal 11.3.3, Drush 13.7.1.0 & PHP 8.3 and patch #41 is not working for me

Thanks in advance!

timohuisman’s picture

Status: Needs work » Needs review
StatusFileSize
new2.69 KB

This patch is a snapshot of MR!14452 so it can be safely used with composer-patches. It applies on D11.3.

Back to NR because NW was only because of the review bot in #56

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests

Issue summary could use some love.

Also appears to be missing test coverage