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?

CommentFileSizeAuthor
#41 interdiff_32-41.txt2.05 KBTanuj.
#41 2950758-41.patch2.43 KBTanuj.
#39 interdiff_32-39.txt5.94 KBTanuj.
#39 2950758-39.patch7.02 KBTanuj.
#36 interdiff_32-36.txt7.36 KBTanuj.
#36 2950758-36.patch8.66 KBTanuj.
#32 2950758-32.patch2.38 KB_utsavsharma
#32 interdiff_31-32.txt729 bytes_utsavsharma
#31 2950758-views-hide-empty-31.patch2.5 KBjcmartinez
#18 2950758-views-hide-empty-18.patch2.37 KBjOpdebeeck
#17 2950758-views-hide-empty-17.patch2.42 KBjOpdebeeck
#8 2950758-views-hide-empty-8.patch1.59 KBmattew
#6 2950758-views-hide-empty-6.patch1.16 KBmattew
#3 2950758-views-hide-empty-3.patch1.07 KBDragoonBoots

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:

Support from Acquia helps fund testing for Drupal Acquia logo

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

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

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

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

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

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

_utsavsharma’s picture

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
FileSize
8.66 KB
7.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
FileSize
7.02 KB
5.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

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);
      }
    }