Views field rewrite replacement subtoken yields double encoded HTML entities

Created on 28 February 2017, over 7 years ago
Updated 11 March 2024, 3 months ago

Problem/Motivation

When a view field has "Override the output of this field with custom text" checked and the rewrite "Text" field includes a sub-token type replacement pattern (which is added by \Drupal\views\Plugin\views\field\EntityField::addSelfTokens()), special characters in the value of that pattern get double encoded. For instance, if the pattern is a title field such as {{ title__value }} with the value "Q&A", the field is ultimately rendered as Q&A, so the end user sees "Q&A" displayed in their browser.

Steps to reproduce

- Create a new content type with image reference field.
- Enable Alt and Title fields for the image.
- Create a content of the newly created content type.
- Provide Image Alt and Title text with some special char like '&'.
- Create a view for this content type with Fields style.
- Add image filed and rewrite the output of this filed to show only the title of the image.
- Check the output, the special char will be shown as encoded entity.

Proposed resolution

Stop encoding the replaced field value in Drupal\views\Plugin\views\field\EntityField::addSelfTokens(), since the value is encoded later in a call to \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace().

Remaining tasks

User interface changes

NA

API changes

NA

Data model changes

NA

Release notes snippet

Views field rewrite replacement subtoken yields double encoded HTML entities

🐛 Bug report
Status

Fixed

Version

10.3

Component
Views 

Last updated about 9 hours ago

Created by

🇮🇳India adhariwal Jaipur

Live updates comments and jobs are added and updated live.
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.

    The issue summary should be updated with the problem and proposed solution specified using the default template. Also since this is causing a UI issue before/after screenshots would be useful.

  • 🇺🇸United States apkwilson

    Updating version to 10.1.x.-dev (which still has this problem), since D9 is EOL.

  • 🇺🇸United States apkwilson

    These patches reroll #22 for Drupal 10.1.x. Tests only patch is expected to fail. The full patch includes the fix and should succeed.

    I'm not familiar with the GitLab system, so I've fallen back to patches.

  • last update 6 months ago
    29,723 pass, 1 fail
  • 🇺🇸United States smustgrave

    Fyi current development branch is 11.x

    What's the issue with gitlab? Eventually patches will be fully phased out so if there's a bug with gitlab we should know

  • 🇺🇸United States apkwilson

    I don't think there's anything wrong with gitlab. I wanted to run the test suite on the commit with just the new test, to show that it failed. In looking around for how to do that, I found this Patch or merge request section of the docs, which seemed to suggest that I should stick with patches here, since that's how the issue started. I'm happy to be corrected.

    I'll reroll again for 11.x and open another merge request - is a test-only commit and test run not really necessary?

  • 🇺🇸United States smustgrave

    Gitlab actually has a test-only feature automatically there. Takes about 1-2 minutes to run.

  • 🇺🇸United States apkwilson

    I did see this test-only job, but it deleted the test view configuration the rerolled patch included, so the test couldn't run and fail the expected way.

    I think I'm going to remove the new test view configuration and use an existing one, so this shouldn't be an issue, here, but what happens when a merge request need to include new test configuration?

  • 🇺🇸United States smustgrave

    If the test only doesn’t fail it’s not a show stopper. Reviewer can always run locally.

  • 🇺🇸United States apkwilson

    I ended up making a new test class and view, after all. I updated their naming to make what their testing more clear.
    Updating the issue title and description to indicate that this is only a problem for field plugin generated subtokens.

  • Status changed to Needs review 6 months ago
  • Status changed to Needs work 6 months ago
  • 🇺🇸United States smustgrave

    Added standard template to the issue summary but left TBD for sections that were missing. If not applicable just change TBD to NA.

  • 🇺🇸United States apkwilson

    Filled out the issue template.

    \Drupal\views\Plugin\views\field\FieldPluginBase::renderText() calls FieldPluginBase::getRenderTokens(), calls addSelfTokens(), which for \Drupal\views\Plugin\views\field\EntityField::addSelfTokens() calls Xss:filterAdmin(), encoding special characters.
    Then renderText() calls FieldPluginBase::renderAltered(), calls \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace(), calls Xss:filterAdmin() (either explicitly or as a post-render callback), encoding special characters a second time.

    The proposed encode/decode process also takes place elsewhere, e.g. in \Drupal\views\Plugin\views\field\FieldPluginBase::renderText() at 1331-1333.
    However, other core field plugins omit encoding the value, and so don't need to decode it. See \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid::addSelfTokens() and \Drupal\user\Plugin\views\field\Roles:addSelfTokens(). Maybe that is a better route, to avoid the extra back-and-forth calls. The value does get filtered in the call to viewsTokenReplace(), anyway.

  • Status changed to Needs review 6 months ago
  • 🇺🇸United States apkwilson

    Removing the encoding seemed a better solution. I've updated the merge request to do that, instead.

  • Status changed to RTBC 6 months ago
  • 🇺🇸United States smustgrave

    Issue summary appears to have been updated

    Ran the test-only feature

    1) Drupal\Tests\views\Kernel\Handler\FieldSelfTokensTest::testSelfTokenEscaping
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'<p>Questions &amp; Answers</p>'
    +'<p>Questions &amp;amp; Answers</p>'
    /builds/issue/drupal-2856598/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:121
    /builds/issue/drupal-2856598/vendor/phpunit/phpunit/src/Framework/Constraint/IsIdentical.php:79
    /builds/issue/drupal-2856598/core/modules/views/tests/src/Kernel/Handler/FieldSelfTokensTest.php:66
    /builds/issue/drupal-2856598/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
    FAILURES!
    Tests: 1, Assertions: 8, Failures: 1.
    

    Which does show the double encoding.

    Change in MR matches issue summary and may change is small so easy to review.

    Think this is good.

  • last update 6 months ago
    29,723 pass, 1 fail
  • last update 5 months ago
    29,723 pass, 1 fail
  • last update 5 months ago
    29,723 pass, 1 fail
  • last update 5 months ago
    29,723 pass, 1 fail
  • last update 5 months ago
    29,723 pass, 1 fail
  • last update 5 months ago
    Build Successful
  • Status changed to Needs work 5 months ago
  • 🇳🇿New Zealand quietone New Zealand

    I'm triaging RTBC issues . I read the IS, the MR and the comments. I didn't find any unanswered questions.

    I did leave some comments in the MR. Setting to NW.

    Leaving at RTBC.

  • Status changed to Needs review 5 months ago
  • 🇺🇸United States apkwilson

    Suggested changes applied. Setting to Needs Review.

  • Status changed to RTBC 5 months ago
  • 🇺🇸United States smustgrave

    Feedback from @quietone appears to be addressed.

  • last update 5 months ago
    29,723 pass, 1 fail
  • last update 5 months ago
    29,723 pass, 1 fail
  • last update 5 months ago
    29,723 pass, 1 fail
  • 55:13
    52:00
    Running
    • catch committed 67674589 on 11.x
      Issue #2856598 by apkwilson, NickDickinsonWilde, ranjith_kumar_k_u,...
    • catch committed 615c69be on 10.3.x
      Issue #2856598 by apkwilson, NickDickinsonWilde, ranjith_kumar_k_u,...
  • Status changed to Fixed 3 months ago
  • 🇬🇧United Kingdom catch

    Double checked that views token replacement does in fact call filterXssAdmin() again and the test coverage looks good.

    Committed/pushed to 11.x and cherry-picked to 10.3.x, thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024