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
| Comment | File | Size | Author |
|---|---|---|---|
| #29 | 2856598-29.patch | 4.68 KB | apkwilson |
| #29 | 2856598-29-tests-only.patch | 3.04 KB | apkwilson |
| #22 | 2856598-20-22.txt | 1.51 KB | yogeshmpawar |
| #22 | 2856598-22.patch | 4.68 KB | yogeshmpawar |
| #20 | interdiff_19-20.txt | 485 bytes | ranjith_kumar_k_u |
Issue fork drupal-2856598
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
Comment #2
adhariwal commentedComment #3
adhariwal commentedComment #4
adhariwal commentedPatch file.
Comment #5
adhariwal commentedComment #6
nileshlohar commentedVerified that this issue exists in Drupal 8.3.x as well
and Re-rolled patch in #4 for 8.3.x
Comment #7
nickdickinsonwildeConfirmed 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.
Comment #8
nickdickinsonwildeNew 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.
Comment #11
nickdickinsonwildehmm 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.
Comment #19
ranjith_kumar_k_u commentedFixed CS errors.
Comment #20
ranjith_kumar_k_u commentedComment #22
yogeshmpawarUpdated patch will fix test failures
Comment #25
smustgrave 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 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.
Comment #26
apkwilson commentedUpdating version to 10.1.x.-dev (which still has this problem), since D9 is EOL.
Comment #29
apkwilson commentedThese 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.
Comment #30
smustgrave commentedFyi 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
Comment #31
apkwilson commentedI 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?
Comment #32
smustgrave commentedGitlab actually has a test-only feature automatically there. Takes about 1-2 minutes to run.
Comment #33
apkwilson commentedI 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?
Comment #34
smustgrave commentedIf the test only doesn’t fail it’s not a show stopper. Reviewer can always run locally.
Comment #36
apkwilson commentedI 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.
Comment #37
apkwilson commentedComment #38
smustgrave commentedAdded standard template to the issue summary but left TBD for sections that were missing. If not applicable just change TBD to NA.
Comment #39
apkwilson commentedFilled out the issue template.
\Drupal\views\Plugin\views\field\FieldPluginBase::renderText()callsFieldPluginBase::getRenderTokens(), callsaddSelfTokens(), which for\Drupal\views\Plugin\views\field\EntityField::addSelfTokens()callsXss:filterAdmin(), encoding special characters.Then
renderText()callsFieldPluginBase::renderAltered(), calls\Drupal\views\Plugin\views\PluginBase::viewsTokenReplace(), callsXss: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 toviewsTokenReplace(), anyway.Comment #40
apkwilson commentedRemoving the encoding seemed a better solution. I've updated the merge request to do that, instead.
Comment #41
smustgrave commentedIssue summary appears to have been updated
Ran the test-only feature
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.
Comment #42
quietone commentedI'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.
Comment #43
apkwilson commentedSuggested changes applied. Setting to Needs Review.
Comment #44
smustgrave commentedFeedback from @quietone appears to be addressed.
Comment #47
catchDouble 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!