Views doesn't parse twig when there are no tokens to replace

Created on 7 March 2017, almost 8 years ago
Updated 4 February 2023, almost 2 years ago

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 )

🐛 Bug report
Status

Needs work

Version

9.5

Component
Views 

Last updated 2 days ago

Created by

🇮🇹Italy boesbo

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    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.

    #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

  • 🇧🇪Belgium Melodia40943

    I tried the patch on php 8.1 and drupal 9.5 but it didn't work, the {{"my text"|t }} still appears as is

  • 🇺🇸United States SocialNicheGuru

    The patch no longer applies to Drupal 9.5.9

  • 🇺🇦Ukraine itaran

    updated patch so it works with Drupal 9.5.9

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Custom Commands Failed
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Custom Commands Failed
  • 🇬🇧United Kingdom jofitz

    Fix coding style error in patch #32.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    30,283 pass, 1 fail
  • 🇺🇸United States el1_1el

    Here'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")

  • 🇺🇦Ukraine taraskorpach Lutsk 🇺🇦

    Just adding a re-roll of #33 for d10.1

  • last update about 1 year ago
    29,630 pass, 1 fail
  • 🇮🇹Italy Giuseppe87

    I'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 :

           {% if 1 > 0 %}         hello       {% endif %}
           {{ ""|t}}
    

    Correctly replace the twig.

    However,

           {% if 1 > 0 %}  hello  {% endif %}
    

    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,

           {% if 1 > 0 %}         hello    {{ ""|t}}     {% endif %}
    

    is replaced by

        {% if 1 > 1 %} hello {% endif %} {{ ""|t}}
    

    Even if writing it in the "source code" mode of CKEditor.

    That cause a server error on the error on the view page:

    Twig\Error\SyntaxError: Unexpected character "&" in "__string_template__f605a68c7565a3509e2a3d658d6b70a7" at line 1. in Twig\Lexer->lexExpression() (line 376 of /var/www/html/vendor/twig/twig/src/Lexer.php).

  • 🇺🇸United States freelock Seattle

    Updated patch for 10.3.0.

  • 🇭🇷Croatia Aporie

    Reroll for 10.3. I think we got lost in the rerolls, #38 didn't work for me.

    Agree with #34.

    Also, have removed unnecessary checks on parameter $tokens which would never be undefined and just wait to be typed into array IMO.

    Let's see if that passes the tests, but from a quick look, I haven't seen any tests regarding parsing of tokens or twig templates. So, we might need a bunch of more tests, to test every area for token parsing when "Use replacement tokens from the first row" is ticked or not.

    Currently, only empty area is checked for global tokens in core/modules/views/tests/src/Functional/Handler/AreaTest.php:testRenderAreaToken, but I might be missing other tests I haven't seen.

    Also guys, I'd like to point/question the necessity of using string translations (such as {{ 'My text'|t }}) in views as they should be translated using entity translation? In my case, I need it to parse twig.

  • 🇮🇳India Athul Dinesh

    I attempted to apply the patch mentioned in comments #38 and #39 on Drupal core version 10.3.0, but unfortunately, it did not work. It seems that the patch is not compatible with this version. Could you please provide an updated patch that is compatible with Drupal 10.3.0?

  • 🇺🇦Ukraine taraskorpach Lutsk 🇺🇦

    #39 has worked for me

  • First commit to issue fork.
  • Pipeline finished with Failed
    5 months ago
    Total: 155s
    #227636
  • Pipeline finished with Failed
    5 months ago
    Total: 657s
    #227654
  • 🇺🇸United States bwong

    I was also able to get #33 working on Drupal 10 manually patching and #39 to work with Drupal 11. Only Drupal 11 has renderInIsolation() so #39 will not work on Drupal 10.

  • Hi,
    MR !8815 has been verified successfully in the Drupal 11.x version. Since all the testcases are passed. Nothing to be update for the functionality.
    Let me know incase of anything need to be update.

    Before Patch:

    After Patch:

Production build 0.71.5 2024