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

  1. Create a view, display page
  2. Add a global unfiltered text field to the view's header
  3. Check use replacement tokens from the first row
  4. Add content with a Twig condition between {% %}
  5. 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 %}

Screenshot of unrendered twig code

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 %}

Screenshot of rendered twig code

Proposed resolution

Twig code contained in a view custom text area field should be executed regardless of whether that code renders a variable using {{ }}

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yioPa created an issue. See original summary.

yioPa’s picture

Proposed solution, adding a patch here.

dagmar’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.1.x-dev
Component: Miscellaneous » views.module
Status: Active » Needs review

Moving to right component/project.

dagmar’s picture

Status: Needs review » Needs work
+++ b/core/modules/views/src/Plugin/views/style/StylePluginBase.php
@@ -231,7 +231,7 @@ public function getRowClass($row_index) {
   public function tokenizeValue($value, $row_index) {

There is another tokenizeValue method on core/modules/views/src/Plugin/views/field/FieldPluginBase.php

Shoud this check be applied there too?

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

nicolas.rafaelli’s picture

Status: Needs work » Needs review
FileSize
1.47 KB

New patch!

dagmar’s picture

Status: Needs review » Needs work
Issue tags: -Twig content views plugins +Need tests

Thanks!. But we need some tests now.

nicolas.rafaelli’s picture

Status: Needs work » Needs review
FileSize
1.58 KB
3.05 KB

Here i upload two new patches.
One with just the test and then one with the test and the old patch.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Need tests

Thanks a lot for adding the tests!

  1. +++ b/core/modules/views_ui/src/Tests/AreaUITest.php
    @@ -0,0 +1,42 @@
    +    $this->drupalPostForm(NULL, ['options[empty]' => 'checked' , 'options[tokenize]' => 'checked', 'options[content]' => '{% if title %} This is the title: {{ title }} {% endif %}'], 'Apply');
    

    Nice, I think

  2. +++ b/core/modules/views_ui/src/Tests/AreaUITest.php
    @@ -0,0 +1,42 @@
    +    $this->assertNoText(t('{% endif %}'));
    +
    +  }
    

    Nitpick solveable before the commit: let's remove this empty line

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/views_ui/src/Tests/AreaUITest.php
@@ -0,0 +1,42 @@
+    $this->assertText(t('This is the title'));
+    $this->assertNoText(t('{% endif %}'));

This shouldn't be wrapped with t() - it's never ever translated.

xeM8VfDh’s picture

Is this fix going to be pushed in the next release?

pashupathi nath gajawada’s picture

Status: Needs work » Needs review
FileSize
3.04 KB

Hi Alex Pott,

As suggested in #10,
I have removed the t functions .

+++ b/core/modules/views_ui/src/Tests/AreaUITest.php
@@ -0,0 +1,42 @@
+    $this->assertText('This is the title');
+    $this->assertNoText('{% endif %}');
dawehner’s picture

How is that a bug? This more reads like a feature for me. Can someone clarify that?

xeM8VfDh’s picture

dawehner, 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!

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

xeM8VfDh’s picture

This seems to be working now in Drupal 8.1.9

dawehner’s picture

@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 ...

xeM8VfDh’s picture

@dawehner If I get a chance to spin up those environments, I will and keep you all posted.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ccmorris’s picture

Status: Needs review » Needs work

This 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.

  1. The included test does not cover the change introduced in the patch. The test replacement includes both {{}} and {%%}. There should be another test replacement with just {%%}. eg. {%if true%}is true{%endif%}
  2. The Twig syntax in "Global: Unfiltered text" does not work when the view does not display fields. eg. Show: Content | Teaser
jonathanshaw’s picture

Title: Views plugins don't accept Twig flow within content » {% %} twig syntax is only parsed in area handler when a {{ }} token is present

#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).

joelpittet’s picture

Issue tags: +Twig
Lendude’s picture

Version: 8.4.x-dev » 8.5.x-dev

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

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

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

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.

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.

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.

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.

ranjith_kumar_k_u’s picture

Just rerolled #12

ranjith_kumar_k_u’s picture

andregp’s picture

Status: Needs work » Needs review
FileSize
4.19 KB
3.5 KB

I 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.

victorml’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed and tested without any problems. Seems good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
    @@ -344,7 +344,7 @@ public function elementClasses($row_index = NULL) {
    -    if (strpos($value, '{{') !== FALSE) {
    +    if (strpos($value, '{{') !== FALSE || strpos($value, '{%') !== FALSE) {
    
    +++ b/core/modules/views/src/Plugin/views/style/StylePluginBase.php
    @@ -228,7 +228,7 @@ public function getRowClass($row_index) {
    -    if (strpos($value, '{{') !== FALSE) {
    +    if (strpos($value, '{{') !== FALSE || strpos($value, '{%') !== FALSE) {
    

    We 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 {%

  2. +++ b/core/modules/views_ui/src/Tests/AreaUITest.php
    @@ -0,0 +1,77 @@
    +  /**
    +   * Undocumented variable.
    +   *
    +   * @var string
    +   */
    +  private $id;
    

    Having test that says Undocumented variable is a bit different. How come we don't say something like Test view ID.

  3. +++ b/core/modules/views_ui/src/Tests/AreaUITest.php
    @@ -0,0 +1,77 @@
    +    $this->drupalGet($view->toUrl('edit-form'));
    

    Given both tests with $this->drupalGet() I think this is not needed.

narendra.rajwar27’s picture

Status: Needs work » Needs review
FileSize
4.16 KB

Updating patch as suggested in comment #38

alexpott’s picture

+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -345,7 +345,7 @@ public function elementClasses($row_index = NULL) {
-    if (strpos($value, '{{') !== FALSE) {
+    if (str_contains($value, '{{') !== FALSE || str_contains($value, '{%') !== FALSE) {

+++ b/core/modules/views/src/Plugin/views/style/StylePluginBase.php
@@ -228,7 +228,7 @@ public function getRowClass($row_index) {
-    if (strpos($value, '{{') !== FALSE) {
+    if (str_contains($value, '{{') !== FALSE || str_contains($value, '{%') !== FALSE) {

The !== FALSE is unnecessary now.

narendra.rajwar27’s picture

@alexpott, thanks.

Updating patch as suggested in comment#40

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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs issue summary update

This 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.

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.

RichardDavies’s picture

Title: {% %} twig syntax is only parsed in area handler when a {{ }} token is present » {% %} twig syntax is only parsed in text area handler when a {{ }} token is present
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update
FileSize
6.07 KB
2.18 KB

Updating the issue summary with more information.

RichardDavies’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Needs work

Patch needs to be updated for 11.x

nitin_lama’s picture

Patch for Drupal 11.x