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
Views uses the hyphen, or dash, character to distinguish some field tokens from sub-field tokens. For example, body and body-summary. Similar to PHP, Twig interprets the hyphen character as a minus sign resulting in a math equation (body - summary) rather than a Twig variable.
Proposed resolution
Replace all hyphens with double-underscores (to avoid confusion with field names containing a single underscore, see #15) in Views tokens.
Remaining tasks
Patch to correct the issue.- Add tests.
- Change record.
User interface changes
Views tokens will use underscores instead of hyphens. This is primarily a UI change, but it will change the definition of exported views as well.
API changes
None.
Novice tasks
Add automated tests:
- Verify that
{{ body-summary }}results in0(Twig should interpret this as a math problem: body minus summary) - Verify that
{{ body__summary }}results in the summary of the body field - Verify that the UI shows replacement options using the double-underscore instead of the single-hyphen
Note: this may require adding a new view or new test data the Views automated test suite. Unfortunately those steps are not documented at the moment.
Original report
After creating new view, I reworte "Body" field as [body-value]. It does not reneder anything.
I tried with [body-summary] and [body-format], and the result is same.
<td class="views-field views-field-body" headers="view-body-table-column">
</td>
Available replacement patterns:
[title] ==
[body] == Body
[body-value] == Raw value
[body-summary] == Raw summary
[body-format] == Raw format
Only if I rewrite as [body] I get rendered output:
<td class="views-field views-field-body" headers="view-body-table-column">
<p>Body test</p>
</td>
| Comment | File | Size | Author |
|---|---|---|---|
| #32 | 2371633-merge-2546210-part2.interdiff.txt | 3.04 KB | mikeker |
| #32 | 2371633-merge-2546210.interdiff.txt | 4.73 KB | mikeker |
| #32 | 2371633-32-tokens-with-hyphens.patch | 10.66 KB | mikeker |
| #25 | 2371633-23-tokens-with-hyphens.patch | 4.18 KB | mikeker |
| #20 | 2371633-20-tokens-with-hyphens.patch | 3.64 KB | mikeker |
Comments
Comment #1
mikeker commentedDuplicate of #2342287: Allow Twig in Views token replacement?
(I haven't look at this issue closely so not changing the status...)
Comment #2
Wuk commentedNo, it's not. That one does not render HTML output properly, but renders something. With this one there is no output at all. I tried also with image field and for
[field_image]there is an output, but for[field_image-width]or[field_image-height]there is no output at all.Comment #3
Wuk commentedI'm sorry for bumping this issue like this, I just made same as in first post, but using twig replacement tokens.
I was using Drupal 8 beta 6 on simplytest.me with standard installation.
Problem is still present, I'm not sure but this issue might have higher priority since there is no output if using replacement patterns.
Comment #4
dawehner@Wuk
So we talk about not just {{field_image}} but rather {{field_image-width}}, right? Just trying to understand exactly what is going on.
The best thing you can do IMHO in views issues is to provide a configuration file of the view, so other people can see, what exactly you have configured.
Thanks a ton.
Comment #5
Wuk commentedYes it's {{field_image-width}}. It's default standard profile installation, no other modules or themes.
After installation I have created few article type nodes, and then made the view.
Here is exported view
Comment #6
dawehnerInteresting, it works for me.
Comment #7
Wuk commentedI tried again today using 8.0.0-beta10 on simplytest.me.
It still does not work for me :(.
When creating simplytest.me installation I used defaults during installation, after that created three basic pages. I have each page body field had one paragraph which i copied from lipsum.com.
After that i created view with two pages, one using rewrite with value {{ body }} for body field, and the other using rewrite with value {{ body-value }} for body field.
I have attached exported view.
@dawehner
Could you, please, try on simplytest.me. It could be version of PHP, missing dependency or something that could be related to web host. I have no idea where to look or what to look for.
Comment #8
mikeker commentedI'm able to reproduce this issue using a rewrite of
To summarize, {{ body-summary }}for the body field. It results in "To summarize, 0" which may not have been output in @Wuk's view as it was seen as empty.@dawehner: There is some strangeness going on here. When I import your view I get:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1') ))) subquery' at line 1: SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression FROM {node} node WHERE (( (.status = :db_condition_placeholder_0) ))) subquery; Array ( [:db_condition_placeholder_0] => 1 )Possibly related to #2487219: target_id is 0 in Entity Ref field when using formatters other than Entity ID?
Comment #9
mikeker commented*groan*
I know what it is... It's the hyphens in the token names -- like PHP, those are interpreted as minus by Twig resulting in bogus token names. That may be the same root issue as the issue linked above, but I haven't look at it closer.
I'll work on a patch that replaces hyphens with underscores in Views' tokens.
Comment #10
mikeker commentedThis will fix the issue for at least some fields. I haven't tested it thoroughly nor gone through the rest of the Views code to see where else we may be using '-' in token names. Would like to see what the testbot thinks first...
Also cleaned up a few other things while I was changing the code:
Technically not in scope, I realize... But the old way of casting an object to an array has issues itself -- the array gets everything in the object including private members, which seems pretty anti-OO.
Comment #12
mikeker commentedUgh... Left a debug call in the patch -- hate it when that happens! :)
Comment #13
mikeker commentedClosing #2487219: target_id is 0 in Entity Ref field when using formatters other than Entity ID as a dup of this and re-titling to better express the root cause.
Comment #14
mikeker commentedMarking as "needs tests" as this is fallout from #2342287: Allow Twig in Views token replacement (and was clearly not caught by tests...)
Added issue summary and beta eval.
Comment #15
dawehnerMh I think I would use double underscore in order to remove confusion with field names including an underscore.
And yeah indeed some form of test coverage would be neat.
Comment #16
star-szrComment #17
mikeker commented#15:
Good call.
Agreed. But I'm having trouble figuring out where we test field plugins for entity reference fields, for example. Any pointers?
Comment #18
mikeker commentedFound another use of the single-hyphen in the docblock for
core/modules/views/src/Plugin/views/field/FieldPluginBase.php::addSelfTokensComment #19
mikeker commentedUpdated issue summary: double-underscores instead of single.
Comment #20
mikeker commentedRerolled.
Comment #21
mikeker commentedComment #22
star-szrThis is looking good, thanks for staying on top of this @mikeker. I like removing the object-to-array cast. Just moving back to needs work for tests.
Comment #23
Wuk commentedIt seems that original issue that I have reported is fixed with patch from #20.
Thank you.
Comment #24
guschilds commentedI came across the same problem when looking into #2453193: {{ field_youtube-input }} and {{ field_youtube-video_id }} in Views are 0 for the YouTube Field module. As this issue mentions, I was previously getting "0" when using tokens with hyphens. After applying the patch from #20 and replacing the hyphens with double underscores, I'm getting correct rewriting results for both YouTube and core field values. Thanks, mikeker et al!
Comment #25
mikeker commentedReroll. Note this should be popped back to NW after the testbot runs -- it still needs automated tests.
Comment #26
mikeker commentedAdded novice tasks sections to the issue summary.
Comment #27
star-szrComment #28
Anonymous (not verified) commentedI added to que for retest but what should be done now on this issue?
Comment #29
mikeker commented@alex_drupal_dev: This patch needs automated tests. See the issue summary for details.
Comment #30
star-szrPossible duplicate but has tests: #2546210: Views subtokens are broken since the introduction of inline templates for replacements
Comment #31
gábor hojtsyAlso #2546210: Views subtokens are broken since the introduction of inline templates for replacements actually modifies the tokens too :) Let's merge efforts! I don't think the issue where the merge happens matters. Here or there. Also definitely not novice.
Comment #32
mikeker commentedI've merged the tests and some of the code from #2546210: Views subtokens are broken since the introduction of inline templates for replacements -- see 2371633-merge-2546210.interdiff.txt. Thanks for pointing me to that issue, @Cottser!
That issue also included the taxonomy and role handler token changes from the other issue, but updated them to show as Twig variables instead of [tokens] -- see the part2 interdiff. (@Gábor Hojtsy, #25 did change the tokens, just not all of them... :)
Agreed, I don't care which issue things more forward in -- I made the changes here since I already had this branch on my local.
Comment #33
mikeker commentedForgot to mention that the merge patch also includes the feedback from #2546210-31: Views subtokens are broken since the introduction of inline templates for replacements.
Comment #34
gábor hojtsyClosing as dupe of #2546210: Views subtokens are broken since the introduction of inline templates for replacements given that that one has tests. @mikeker: can you post the merged patch there to ensure you get the credit too? Thanks a lot!