Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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?
Comment | File | Size | Author |
---|---|---|---|
#41 | interdiff_32-41.txt | 2.05 KB | Tanuj. |
#41 | 2950758-41.patch | 2.43 KB | Tanuj. |
#39 | interdiff_32-39.txt | 5.94 KB | Tanuj. |
#39 | 2950758-39.patch | 7.02 KB | Tanuj. |
#32 | 2950758-32.patch | 2.38 KB | _utsavsharma |
Issue fork drupal-2950758
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
Comment #2
AnybodyComment #3
DragoonBootsI 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.
Comment #4
DragoonBootsComment #6
mattew CreditAttribution: mattew commentedAdd a test to check if the field has been excluded.
Comment #7
AnybodyWell 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.
Comment #8
mattew CreditAttribution: mattew commentedHere is an updated patch which contains the test against debug mode status.
Comment #9
AnybodyWell 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?
Comment #10
mattew CreditAttribution: mattew commentedI'm not quite sure if the regex is totally bullet-proof to exclude HTML comments. Maybe somebody could check this?
Comment #11
mattew CreditAttribution: mattew commentedOh, 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.
Comment #13
ivnish CreditAttribution: ivnish commentedIt'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
Comment #14
emb03 CreditAttribution: emb03 commentedYes, I can confirm this is a bug in views-view-fields as well in support of #13
Comment #15
r0nn1ef CreditAttribution: r0nn1ef as a volunteer and commentedThis also applies if the title for the display is not empty on Drupal 8.7.7.
Comment #17
jOpdebeeck CreditAttribution: jOpdebeeck commentedI 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)
Comment #18
jOpdebeeck CreditAttribution: jOpdebeeck commentedI remade the patch because it failed to apply.
Comment #20
LendudeThis 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.
Comment #21
Ruslan PiskarovUnfortunately #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.
Comment #28
dpiThis 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
Comment #31
jcmartinezI 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.
Comment #32
_utsavsharma CreditAttribution: _utsavsharma at OpenSense Labs for DrupalFit commentedTried to fix CCF for #31.
Comment #33
jwilson3Comment #34
dpi32 incorrectly introduces a space in the regex.
Comment #35
dpiNoticed a problem with the patch. The following error is raised, likely due to PHP8 changes.
We should ensure we don't enter the
if (\Drupal::service('twig')->isDebug()) {
condition if value isNULL
.Comment #36
Tanuj. CreditAttribution: Tanuj. as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedAdding a new patch addressing points from #34 and #35, and also fixing some phpcs errors. please review.
Comment #38
dpithe 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.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
Comment #39
Tanuj. CreditAttribution: Tanuj. as a volunteer and at Srijan | A Material+ Company for Drupal India Association commented@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"
Comment #40
dpiPlease address the CS changes relevant to the lines being modified only.
Comment #41
Tanuj. CreditAttribution: Tanuj. as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedupdated according to #40, please review.
Comment #44
vasike@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: