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.
i have this configuration:
if there are no results the view print: {{ 'News'|t }}
if there are results instead it is correctly printed
(this issue don't fix this problem: https://www.drupal.org/node/2610236)
Comment | File | Size | Author |
---|---|---|---|
#35 | 2858392-35.patch | 5.56 KB | taraskorpach |
#34 | 2858392-34.patch | 1.48 KB | el1_1el |
#33 | 2858392-33.patch | 5.65 KB | jofitz |
#33 | interdiff-2858392-32-33.txt | 371 bytes | jofitz |
#32 | 2858392-15.patch | 5.67 KB | itaran |
Comments
Comment #2
boesbo CreditAttribution: boesbo as a volunteer commentedComment #3
boesbo CreditAttribution: boesbo as a volunteer commentedComment #4
joshi.rohit100I think there needs some discussion around what to be shown if no data as then token will have nothing to replace. Should be show as it is (no change) or just remove or return no data (empty).
Comment #5
boesbo CreditAttribution: boesbo as a volunteer commented! @joshi, so there is no way to insert strings with T?
Comment #6
LendudeSeems to touch on #2665766: {% %} twig syntax is only parsed in text area handler when a {{ }} token is present
Comment #7
jonathanshawAgreed. We've got 4 choices:
1) Always parse twig, but set first row as variables for twig depending on this UI "Use replacement tokens from first row" setting
2) Always parse twig, and always add the first row as variables, abandon the UI setting
3) Make a new setting additional "Process the input using Twig", and only offer the "Use replacement tokens from first row" if that is checked
4) Replace the current setting with a single "Process the input using Twig" setting
I'd suggest #2 is best - what's the point of these UI settings? Everything is presumably cached to the 11th degree, so I imagine there's no performance issue, and as long as we support Twig escaping then the twig parsing is not blocking anything a sitebuilder might want to do.
Comment #8
Pranali.addweb CreditAttribution: Pranali.addweb at AddWeb Solution Pvt. Ltd. commentedTwig syntax would only be parsed when the data is available or the token is available or present within the syntax. Please try these steps:
1.create a view and display it.
2.Should add a global filter text field to the header.
3.Replace the token with the first row. The first row as a variable should work properly.
If the data filter has nothing to replace if no data, then we should set something like no data or empty value or a message.
Comment #9
Ivan Berezhnov CreditAttribution: Ivan Berezhnov as a volunteer and at Drupal Ukraine Community for Levi9 commentedComment #10
hanoiiI think this is still valid if you want to use some twig syntax on a general header, even to be shown regardless of results/no results.
Comment #11
jonathanshawAgreed with #10. Always processing Twig empowers sitebuilders more, provides more consistency for developers, and makes for a simper easier-to-maintain and test codebase.
Comment #12
hanoiiAnd also, when it says that it works "simliar to the global text on fields", there you can use twig, so that's what I was surprised when it didin't work there.
Comment #13
hanoiiI just want to comment that I kind of sorted this out by using: https://www.drupal.org/project/twig
Comment #14
Sut3kh CreditAttribution: Sut3kh as a volunteer commentedSo it looks like the inconsistency is simply due to a micro-optimisation to quick return when there are no tokens.
This actually covers more scenarios than simply having no results (such as showing 'Content' instead of 'Fields') so I have updated the title accordingly.
I would say that the expected behaviour of checking 'Use replacement tokens from the first row' is that it should always render twig (rather than render twig in certain conditions but sometimes dump the twig as plain text) so i have attached a patch to fix this 'bug'.
IMO we need Twig to be able to do things 'properly' in views, especially with no results i.e. adding a simple 'Add node' link to the header:
<a href="{{ path('node.add_page') }}">{{ 'Add Node'|t }}</a>
This should probably have a simple unit test and the checkbox should be updated to explicitly state that it well render Twig as well but I'm afraid I don't have any more time for this now sorry.
I think there probably is merit in the higher level discussion around this checkbox but for now though I would call this is a simple bug in it's current state and further high-level discussion should probably go to a new issue so this can progress for now (especially as any changes to the checkbox are likely to be a breaking and therefore better suited for D9).
Comment #15
handkerchiefPatch from #14 does not work for me. Only visible change: The raw twig token is no longer displayed on the final empty view.
Comment #16
MurzI have the problem with Views 8.6, when Views doesn't parse twig in header text, when View have no argument in url, if I fill argument - twig works, if remove - stops working. Is this same issue, or other?
Comment #19
jrearickI was trying to get the "Add Page" link in the header to work with the twig patterns, and it seems I ran into this issue. Applying the patch in #14 seems to have solved the issue for me.
Steps to reproduce from a fresh install of Drupal 8.9 standard profile with no nodes:
I'm not sure what's next here. I think maybe running the existing tests to make sure it doesn't cause regressions and developing tests for this issue? Neither of which I am familiar with how to do.
Comment #20
jrearickNeeds Reivew to run the patch through current tests.
Comment #21
Mistrae CreditAttribution: Mistrae commentedTested on 9.1.8, the twig in header/footer is not parsed (my view displays teasers)
Comment #22
nikathonePatch #14 works for me when adding the "Global: Unfiltered text" under "NO RESULTS BEHAVIOR".
Comment #25
el1_1el CreditAttribution: el1_1el commented#14 works for me in both header and no results behavior using unfiltered text with fields or teasers in 9.3.14 with the following twig that does require the twig tweak module
Setting back to needs review since it looks like this has been an issue for a while
Comment #27
bwong CreditAttribution: bwong as a volunteer commented#14 works for me in header, footer and no results using unfiltered text with fields in 9.3.14.
What I found odd is that I need to have a substitution that works before full twig works. Otherwise I wind up with the unevaluated twig expression. This could be an issue with twig substitution rather than the this patch.
Comment #29
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis 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.
#14 didn't work for me on Drupal 10.1
Added a test area to a view
Added {{ 'fail'|trans }} to the text area
Would expect to see nothing but renders like I do if I place the same code in a twig template.
Also was previously tagged for tests so that will still need to happen as well
Comment #30
Melodia40943 CreditAttribution: Melodia40943 at WebstanZ commentedI tried the patch on php 8.1 and drupal 9.5 but it didn't work, the {{"my text"|t }} still appears as is
Comment #31
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedThe patch no longer applies to Drupal 9.5.9
Comment #32
itaran CreditAttribution: itaran commentedupdated patch so it works with Drupal 9.5.9
Comment #33
jofitz CreditAttribution: jofitz at SIRUSS commentedFix coding style error in patch #32.
Comment #34
el1_1el CreditAttribution: el1_1el commentedHere's a patch for 10.1.3 - yes it has coding style errors (its not going to be committed w/o tests anyway so ¯\_(ツ)_/¯ ).
Patch is designed to show that running $text through Xss::filterAdmin when the $tokens var is empty is what causes the issue. I may be totally mistaken or misunderstanding something, but the $build runs the xss filter anyway at
return Xss::filterAdmin($children);
, so I'm not entirely clear why showing raw twig to users when results are empty is necessary but again ¯\_(ツ)_/¯And moreover - patch is to allow people who have this issue to update to 10.1.x without showing twig syntax to users (ie what my PM/UX manager referred to as "cool gobbly gook")
Comment #35
taraskorpach CreditAttribution: taraskorpach as a volunteer and at Drupal Ukraine Community commentedJust adding a re-roll of #33 for d10.1
Comment #36
Giuseppe87 CreditAttribution: Giuseppe87 commentedI've tried the patch on D10.1.x but it seems it has some problems:
If I print in the view header using unfiltered text :
Correctly replace the twig.
However,
Does not replace the twig.
What's more, if I use the Global Text Area, and thus a input area with a text format and CKEditor,
is replaced by
Even if writing it in the "source code" mode of CKEditor (the > is html escaped).
That cause a server error on the error on the view page: