Beta phase evaluation
Issue category | Bug: Views tokens available in D7 are now missing |
---|---|
Issue priority | Major: Followup from #2342287: Allow Twig in Views token replacement |
Prioritized changes | "Follow-up from a recent critical or major change" |
Disruption | Minimal: Few modules provide Views tokens. The change is simply replacing a hyphen with a double-underscore. |
Problem/Motivation
When using the views overrides placeholders in fields, they don't render the correct values.
The reason is that we are using inline templates for rendering them (actually we allow to use twig there), but the subtokens are formed by using dashes ('-'). This makes them invalid twig variable names, which instead of throwing an error makes twig understand them as a mathematical expression.
Proposed resolution
Replace the field - subfield separator. Instead of using a dash we should use a double underscore.
E.g.: {{ langcode-value}} becomes {{ langcode__value}}
Remaining tasks
Review. Commit.
User interface changes
The tokens available change when overriding a field change.
API changes
The tokens available change when overriding a field change.
Data model changes
None.
Original report:
When using the {{ langcode }} placeholder, it renders the language name not the code. That is a bit obscure but one would expect, its the field rendered. Maybe the field should have settings to be able to render as the langcode. But then there is the {{ langcode-value }} token which was promising because it should definitely render the raw value. That renders numbers though.
Comment | File | Size | Author |
---|---|---|---|
#45 | 2546210-43-45.interdiff.txt | 1.18 KB | mikeker |
#45 | 2546210-views-tokens-45.patch | 22.28 KB | mikeker |
#43 | 2546210-41-43.interdiff.txt | 7.48 KB | mikeker |
#43 | 2546210-views-tokens-43.patch | 22.21 KB | mikeker |
#41 | 2546210-40-41.interdiff.txt | 5.57 KB | mikeker |
Comments
Comment #2
Gábor HojtsyComment #3
Gábor HojtsyComment #4
Gábor HojtsyComment #5
penyaskitoTrying to reproduce this. If I understood correctly, for reproducing it I need to edit the "Content" view, add the language field, add a "Global: Custom Text" field and use
{{ langcode-value }}
as a replacement.@Gábor: Where did you get that you could use "{{ langcode-value }}"? I don't see it listed at the replacements section. Actually, if you add anything (e.g.
{{ langcode-value-unknown }}
, you get a "0". This is a bug for sure, but maybe a different one than expected.Comment #6
Gábor Hojtsy[2:39pm] GaborHojtsy: penyaskito: well, its in the replacement values for the **langcode field itself**
[2:39pm] GaborHojtsy: penyaskito: so you can rewrite the output of teh langcode field with placeholders
[2:39pm] GaborHojtsy: penyaskito: THAT offers up that placeholder
[2:39pm] GaborHojtsy: penyaskito: for some reason not other places
[2:42pm] GaborHojtsy: penyaskito: I wanted to use it in URLs of a link on another field to pass on language result information in a link for example
Comment #7
penyaskitoI was able of reproducing it, working on a potential patch
Comment #8
penyaskitoThe problem is that we are using inline_templates for replacements and we are composing our tokens by concatenating fields and columns with hyphens.
But a twig variable name cannot contain a hyphen, so its evaluation is failing and we are getting wrong values.
I guess it's too late in the release cycle for changing how we compose the tokens, and it will be a change from how D7 worked.
Let's see if a simple substitution works.
Comment #10
penyaskitoOk, let's replace the token completely. But in that case, I'm wondering if we should rely on twig at all.
Comment #11
penyaskitoGo testbot!
Comment #13
penyaskitoAnd now for real, sorry for the noise.
Comment #14
penyaskitoTalked with Gábor about this. As the overwrite field description says that twig is allowed, people aware of twig variable naming restrictions can find out that the twig code we are forcing them to write for the overrides is invalid, so we should accept this API change, as anything else is even more problematic.
Also, in the description of the langcode-value we specify "raw value", but taking into account that twig has autoescape enabled, that's not true anymore. If we want to render the raw value we are forced to use
{{ langcode|raw }}
Comment #15
Gábor HojtsyWell, not {{ langcode|raw }} because langcode is the rendered version of the language field, so will be the NAME of the language. It would need to be {{ langcode-raw|raw }} because langcode-raw is the raw value and |raw makes it not escaped in Twig. (This one is not an issue for the langcode itself because it will not be able to include things to escape, but for other raw values, yeah).
Comment #16
penyaskitoThanks for clarifying.
New attempt, now by replacing the tokens separator with two underscores ("__"), which makes it a valid twig variable.
Comment #17
penyaskitoComment #18
dawehnerWow, yeah I think the right fix is to use __ instead of just _
As we already know, we need tests.
Comment #19
penyaskitoAdded a test for token replacements.
I include test-only patch with the tokens using dashes so we can see how it fails.
Comment #20
penyaskito[duped]
Comment #22
penyaskitoNo, we can't do that :(
Comment #23
penyaskito@dawehner told me how to fix + test my issue with rendering without a render context. This should be green, and tests included should suffice.
Comment #24
Gábor HojtsyLooks good to me. Made minor adjustments to issue summary. Thanks for sticking to it and resolving it.
Comment #25
alexpottI think we should have a CR for this and an
assert()
in whichever method gathers all the tokens that they don't contain a - or any other illegal twig variable characters.Comment #26
penyaskitoDrafted the change record.
Comment #27
Gábor HojtsyUpdated change notice: https://www.drupal.org/node/2548291/revisions/view/8762147/8762167
Comment #28
penyaskitoAdded assertion. I couldn't find any docs on twig about how variable naming is validated, aside of that they cannot contain dashes. I added the regexp from PHP variable names validation.
Comment #29
star-szrNice to see that this is further along, however I think it's a duplicate of #2371633: Views uses hyphens in many tokens, but that is not valid Twig syntax.. Might be good to compare notes before closing one as a dupe.
Comment #30
penyaskitoNitpick:
Missing space after the if.
Comment #31
dawehnerGreat, now we have a test for an actual token! Here are some small bits.
Could we point to some place in twig so in case they change we at least know where the regex was coming from?
Nitpick: Space after [
Two empty spaces
You don't need an extra namespace, ... you can keep the existing one of the test
Comment #32
mikeker CreditAttribution: mikeker as a volunteer commentedI merged the test and taxonomy/role handler changes in this issue into a new patch in #2371633: Views uses hyphens in many tokens, but that is not valid Twig syntax..
As mentioned in that issue, I don't really care which issue moves forward and which is marked as a dup -- the solutions are nearly identical. I things do move forward in this issue, I would like to see the removal of the object-to-array cast and the
CoreXss
namespace alias merged into this issue.@penyaskito, thank you for your work on this and scaffolding the tests! I had stalled on that in the other issue and hadn't had time to return to it.
Comment #33
Gábor HojtsyClosed #2371633: Views uses hyphens in many tokens, but that is not valid Twig syntax. as a dupe.
Comment #34
Gábor HojtsyMerged tags.
Comment #35
dawehnerComment #36
mikeker CreditAttribution: mikeker as a volunteer commentedAdded beta-eval from #2371633: Views uses hyphens in many tokens, but that is not valid Twig syntax..
Comment #37
mikeker CreditAttribution: mikeker as a volunteer commentedReferenced in dup issue.
Comment #38
mikeker CreditAttribution: mikeker as a volunteer commentedMerged patch from #2371633-32: Views uses hyphens in many tokens, but that is not valid Twig syntax.. Interdiff is against the patch in #28 of this issue -- interdiffs against the other issue can be found in the first link.
As mentioned in #32, this patch includes the cleanup from the dup issue: removing the object-to-array cast and the extraneous CoreXss namespace alias. I would like to see more tests to cover the original bug report in the dup issue -- I'll work on those later today.
Comment #39
penyaskitoIs this a valid variable?
Does that str_replace add - instead of removing them?
Comment #40
mikeker CreditAttribution: mikeker as a volunteer commentedNope.
Yup.
Thanks for the review @penyaskito. Sorry, I dorked those up when merging the two patches. The fact that the testbot gave us a green light tells me that we need better test coverage around this!
Comment #41
mikeker CreditAttribution: mikeker as a volunteer commentedAdds tests for the all-terms field handler tokens (those from
TaxonomyIndexTid
).Comment #42
Gábor HojtsyLooks good for me! Great that we could merge efforts. Let's get it in then!
Comment #43
mikeker CreditAttribution: mikeker as a volunteer commentedHate to remove an RTBC, but I wanted to get some tests in that cover the original report in #2371633: Views uses hyphens in many tokens, but that is not valid Twig syntax.. Sorry, took longer than expected to get back to this...
This patch adds tests to the Node module for the tokens
__value
,__summary
, and__format
tokens.Comment #44
stefan.r CreditAttribution: stefan.r commentedShould we refer to this by the namespaced class name?
Nit: bracket notation
Missing method doc
Comment #45
mikeker CreditAttribution: mikeker as a volunteer commented@stefan.r: Thanks for the review. Fixed.
Comment #46
stefan.r CreditAttribution: stefan.r commentedExtra test coverage can only be a good thing! Haven't reviewed the complete patch but RTBCing considering #41 was RTBC and the test looks good.
Comment #47
Gábor HojtsyYay, thanks for expanding coverage!
Comment #48
alexpottThis issue is a normal bug fix, and disruption is necessary to fix the bug and has a CR, so it is allowed per https://www.drupal.org/core/beta-changes. Committed f71a375 and pushed to 8.0.x. Thanks!
Comment #50
Gábor HojtsyYay, amazing, thanks!