Problem/Motivation
When adding a custom text area field to a view with twig conditions ({% if condition %}
) or twig flow control ({% for X in Y %}
), it's not getting processed.
Steps to reproduce
- Create a view, display page
- Add a global unfiltered text field to the view's header
- Check use replacement tokens from the first row
- Add content with a Twig condition between
{% %}
- Go to the view page and you'll see your twig code within the HTML
For example, assuming you've added your "Content type" field as one of the fields in your view, this Twig will not be executed and will instead just render the code as plain text:
{% if type matches '/Page/' %}
<p>Twig Test</p>
{% endif %}
However, if we also output a field using {{ field_name }} somewhere within the code, then it will execute the Twig code:
{% if type matches '/Page/' %}
<p>Twig Test: {{ type }}</p>
{% endif %}
Proposed resolution
Twig code contained in a view custom text area field should be executed regardless of whether that code renders a variable using {{ }}
Comment | File | Size | Author |
---|---|---|---|
#48 | 2665766-48.patch | 4.11 KB | nitin_lama |
#45 | Rendered Twig code.png | 2.18 KB | RichardDavies |
#45 | Unrendered Twig code.png | 6.07 KB | RichardDavies |
#41 | interdiff_39-41.txt | 1.34 KB | narendra.rajwar27 |
#41 | 2665766-41.patch | 4.12 KB | narendra.rajwar27 |
Comments
Comment #2
yioPa CreditAttribution: yioPa at Globant commentedProposed solution, adding a patch here.
Comment #3
dagmarMoving to right component/project.
Comment #4
dagmarThere is another tokenizeValue method on
core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Shoud this check be applied there too?
Comment #6
nicolas.rafaelli CreditAttribution: nicolas.rafaelli at Globant commentedNew patch!
Comment #7
dagmarThanks!. But we need some tests now.
Comment #8
nicolas.rafaelli CreditAttribution: nicolas.rafaelli at Globant commentedHere i upload two new patches.
One with just the test and then one with the test and the old patch.
Comment #9
dawehnerThanks a lot for adding the tests!
Nice, I think
Nitpick solveable before the commit: let's remove this empty line
Comment #10
alexpottThis shouldn't be wrapped with t() - it's never ever translated.
Comment #11
xeM8VfDh CreditAttribution: xeM8VfDh commentedIs this fix going to be pushed in the next release?
Comment #12
pashupathi nath gajawada CreditAttribution: pashupathi nath gajawada as a volunteer and at Melity commentedHi Alex Pott,
As suggested in #10,
I have removed the t functions .
Comment #13
dawehnerHow is that a bug? This more reads like a feature for me. Can someone clarify that?
Comment #14
xeM8VfDh CreditAttribution: xeM8VfDh commenteddawehner, I can't really speak for the original person who opened the ticket, but the issue for us is that we cannot use TWIG within the "Global: Unfiltered text" field type within, for example, the HEADER section. We can and do use TWIG in "Global: Custom text" fields within the FIELDS section, which is incredibly useful. We'd like to dynamically build our header content based on the page/view's context, but are simply unable to do that right now because of this issue. As a result, we have to build multiple separate views that are the exact same except for their different hard-coded header HTML, when we should be able to consolidate them into a single view that dynamically generates the header HTML (via TWIG) based on the context. IMO, the more places we can use TWIG within the UI, the better!
Comment #16
xeM8VfDh CreditAttribution: xeM8VfDh commentedThis seems to be working now in Drupal 8.1.9
Comment #17
dawehner@xeM8VfDh
Thank you for trying it out again! I'm glad that this works now. Do you mind trying out 8.2.x and maybe even 8.3.x
It would be great to be 100% sure that stuff is actually working ...
Comment #18
xeM8VfDh CreditAttribution: xeM8VfDh commented@dawehner If I get a chance to spin up those environments, I will and keep you all posted.
Comment #20
ccmorris CreditAttribution: ccmorris commentedThis issue is not fixed just yet. Tested on a fresh stable (8.2.6) and on 8.4.x.
The patch in #12 fixes the basic case. There are some problems with the patch though.
Comment #21
jonathanshaw#20.1 Very true, having a test of what happens without {{ }} is crucial
#20.2 That should be a separate issue? (although have you tried the 'Force to use fields' setting and a hidden field?)
It also raises a bigger question, of why do we have to check "use tokens from first row" to enable Twig parsing at all. It's perfectly possible to use Twig for things that don't require field values, like showing/hiding something depending on the current day of the week. Ultimately perhaps token replacement needs to be treated as an aspect of twig parsing. But it looks like that kind of bigger refactoring should be part of #2396607: Allow Views token matching for validation (and remove dead code).
Comment #22
joelpittetComment #23
LendudeThis seems related/touching on #2858392: Views doesn't parse twig when there are no tokens to replace
Comment #34
ranjith_kumar_k_u CreditAttribution: ranjith_kumar_k_u at Zyxware Technologies commentedJust rerolled #12
Comment #35
ranjith_kumar_k_u CreditAttribution: ranjith_kumar_k_u at Zyxware Technologies commentedComment #36
andregp CreditAttribution: andregp at CI&T commentedI added a
use Drupal\Tests\views_ui\Functional\UITestBase;
statement in the test (so it would actually run) and replaced the deprecated functions on the test.I also tried to address the comment #21.
Comment #37
victorml CreditAttribution: victorml commentedReviewed and tested without any problems. Seems good to me.
Comment #38
alexpottWe should use
str_contains()
it's more readable and comes with PHP 8.0 (and we have a polyfill for PHP 7 in D9).But also are we concerned about existing views which already have {% in the text but don't want it to be interpreted as Twig. This feels very tricky. Not sure what to do here because I've definitely been caught out by assuming {%
Having test that says Undocumented variable is a bit different. How come we don't say something like Test view ID.
Given both tests with $this->drupalGet() I think this is not needed.
Comment #39
narendra.rajwar27Updating patch as suggested in comment #38
Comment #40
alexpottThe
!== FALSE
is unnecessary now.Comment #41
narendra.rajwar27@alexpott, thanks.
Updating patch as suggested in comment#40
Comment #43
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.
The issue summary could use an update using the default template. What was the proposed solution to fix the problem? Screenshots of the error would be useful too as this seems to be a UX issue.
Comment #45
RichardDavies CreditAttribution: RichardDavies at City of Portland commentedUpdating the issue summary with more information.
Comment #46
RichardDavies CreditAttribution: RichardDavies at City of Portland commentedComment #47
smustgrave CreditAttribution: smustgrave at Mobomo commentedPatch needs to be updated for 11.x
Comment #48
nitin_lamaPatch for Drupal 11.x