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

Update summary

User interface changes

NA

API changes

NA

Data model changes

NA

Release notes snippet

Views field rewrite replacement subtoken yields double encoded HTML entities

Issue fork drupal-2856598

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

adhariwal created an issue. See original summary.

adhariwal’s picture

adhariwal’s picture

adhariwal’s picture

adhariwal’s picture

Status: Active » Needs review
nileshlohar’s picture

Version: 8.2.x-dev » 8.3.x-dev
StatusFileSize
new1.71 KB

Verified that this issue exists in Drupal 8.3.x as well
and Re-rolled patch in #4 for 8.3.x

nickdickinsonwilde’s picture

Version: 8.3.x-dev » 8.4.x-dev
Assigned: Unassigned » nickdickinsonwilde
Status: Needs review » Needs work
Issue tags: +Needs tests

Confirmed that the patch works as expected.
However, it does need a test. Just working my head around it how to test it, should be easy enough. I hope.

nickdickinsonwilde’s picture

Assigned: nickdickinsonwilde » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.33 KB
new4.96 KB

New test & test only patch.
Notes: Due to adding an HTML entity in the test data, a couple other tests had to be adjusted. I believe this is reasonable but an extra look should be given there.

The last submitted patch, 8: 2856598-views_replacement_token-test-only-8.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 8: 2856598-views_replacement_token-8.patch, failed testing. View results

nickdickinsonwilde’s picture

hmm that had unexpected side effects on a lot of other tests. Worse yet, I found that it didn't fail when it should have.
New test created that doesn't interfere and actually works.

The last submitted patch, 11: 2856598-views_replacement_token-test-only-11.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
ranjith_kumar_k_u’s picture

StatusFileSize
new4.67 KB
new1.55 KB

Fixed CS errors.

ranjith_kumar_k_u’s picture

StatusFileSize
new4.67 KB
new485 bytes

Status: Needs review » Needs work

The last submitted patch, 20: 2856598-20.patch, failed testing. View results

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new4.68 KB
new1.51 KB

Updated patch will fix test failures

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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 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.

apkwilson’s picture

Title: Views replacement token for fields shows encoded htmlentities. » Views field rewrite replacement pattern yields double encoded HTML entities
Version: 9.5.x-dev » 10.1.x-dev
Issue summary: View changes

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

apkwilson’s picture

StatusFileSize
new3.04 KB
new4.68 KB

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.

smustgrave’s picture

Version: 10.1.x-dev » 11.x-dev

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

apkwilson’s picture

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?

smustgrave’s picture

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

apkwilson’s picture

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 does need to include new test configuration?

smustgrave’s picture

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

apkwilson’s picture

Title: Views field rewrite replacement pattern yields double encoded HTML entities » Views field rewrite replacement subtoken yields double encoded HTML entities
Issue summary: View changes

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

apkwilson’s picture

Status: Needs work » Needs review
smustgrave’s picture

Issue summary: View changes
Status: Needs review » Needs work

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

apkwilson’s picture

Issue summary: View changes

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.

apkwilson’s picture

Issue summary: View changes
Status: Needs work » Needs review

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

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update

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.

quietone’s picture

Status: Reviewed & tested by the community » Needs work

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.

apkwilson’s picture

Status: Needs work » Needs review

Suggested changes applied. Setting to Needs Review.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback from @quietone appears to be addressed.

  • 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,...
catch’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed

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!

Status: Fixed » Closed (fixed)

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