Using D8-beta13 I attempted to use a token in a view to rewrite the CSS output:
To reproduce:
- Add text field (plain) to a view.
- Under Style Settings in the field configuration, select 'create a css class'
- Insert:
something-{{ field_fieldname }} heading
where '{{ field_fieldname }}' is the replacement token grabbed from the 'rewrite results' section. - Save
- Load a page with this view on it (in my case, it was a page displaying the blocks of the view.
Expected:
Line would be enclosed in css like this: <i something-fieldcontent heading>whatever</i>
This does not fail in the view preview. It renders as expected there. However,
When a page with the block/view is loaded, the "website encountered an unexpected error". Console output shows:
[Thu Jul 30 16:52:36.495200 2015] [fcgid:warn] [pid 17226:tid 3015823360] [client 127.0.0.1:52474] mod_fcgid: stderr: Uncaught PHP Exception Twig_Error_Syntax: "Unexpected token "end of template" of value "" in "{# inline_template_start #}something-{{" at line 1" at /core/vendor/twig/twig/lib/Twig/ExpressionParser.php line 190, referer: http://local.site/admin/structure/views/view/services
Note:
1. The same replacement token works without error in the "Rewrite Results" section. I.e., when no css is involved.
2. CSS replacement works if it does not contain the token.
Comment | File | Size | Author |
---|---|---|---|
#8 | 2543796-tests-only.patch | 2.03 KB | mikeker |
#8 | 2543796-8-views-rewrite-css.patch | 3.9 KB | mikeker |
#6 | twig-2543796-6.patch | 730 bytes | therealssj |
Comments
Comment #1
mikeker CreditAttribution: mikeker as a volunteer commentedThis is a bug, no reason to push it to 8.1.x yet.
Comment #2
Ameer Khan CreditAttribution: Ameer Khan as a volunteer commentedI am also facing same issue in drupal-8.0.5. Is there any patch available?
Comment #3
rootworkComment #4
mikeker CreditAttribution: mikeker as a volunteer commentedThe issue is that we take the string entered in the "CSS class" field and break it up by whitespaces to set those as classes on the element. If you have a whitespace in your token (eg:
{{ foo }}
instead of{{foo}}
) then the token gets broken up resulting in a Twig syntax error.(Note, it's so much that it works correctly in preview, but that preview doesn't add those classes.)
So the workaround is to remove whitespaces from your tokens. Because there's a feasible workaround, I'm bumping this down to minor.
To fix the issue, we should either add some help text explaining this -- if you copy/paste the token from the replacement values section you will see this issue which is terrible UX -- or change
FieldPluginBase::elementClasses
to respect Twig token notation when exploding the string.Comment #5
therealssj CreditAttribution: therealssj commentedHow about this?
Comment #6
therealssj CreditAttribution: therealssj commentedFor 8.1.x,
I hope this fixes the issue
Comment #7
therealssj CreditAttribution: therealssj commentedComment #8
mikeker CreditAttribution: mikeker as a volunteer commentedSorry, I ended up working on this in parallel to @therealssj, but didn't assign the issue to me so we've duplicated some effort here. Rather than a regex, I'd rather we tokenize the entire class string and then explode/clean/implode it.
Also added unit tests and handles
elementLabelClasses
andelementWrapperClasses
.Comment #9
mikeker CreditAttribution: mikeker as a volunteer commentedComment #11
mikeker CreditAttribution: mikeker as a volunteer commentedOne of these days I'll remember to upload the test-only patch first...
Comment #12
mikeker CreditAttribution: mikeker as a volunteer commentedComment #13
star-szrI don't think this fits under minor, seems like it would be easy enough to run into this.
Patch is looking good I'd say :)
Comment #15
ibuildit CreditAttribution: ibuildit commentedI'm still having the bug, running latest, and the patches doesn't help. :(
Comment #16
ibuildit CreditAttribution: ibuildit commentedSorry, my bad. The patch is working.
Comment #18
el1_1el CreditAttribution: el1_1el commented#8 works for me. Thanks!
Comment #21
catchCommitted/pushed to 8.4.x and cherry-picked to 8.3.x. Thanks!
Comment #22
rootworkAwesome, thank you!