Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
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:

  1. Verify that {{ body-summary }} results in 0 (Twig should interpret this as a math problem: body minus summary)
  2. Verify that {{ body__summary }} results in the summary of the body field
  3. 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>
Files: 
CommentFileSizeAuthor
#32 2371633-merge-2546210-part2.interdiff.txt3.04 KBmikeker
#32 2371633-merge-2546210.interdiff.txt4.73 KBmikeker
#32 2371633-32-tokens-with-hyphens.patch10.66 KBmikeker
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 103,213 pass(es). View
#25 2371633-23-tokens-with-hyphens.patch4.18 KBmikeker
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 99,832 pass(es). View
#20 2371633-20-tokens-with-hyphens.patch3.64 KBmikeker
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 96,899 pass(es). View
#18 2371633-17-18.interdiff.txt1 KBmikeker
#18 2371633-18-tokens-with-hyphens.patch3.63 KBmikeker
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 96,340 pass(es). View
#17 2371633-12-17.interdiff.txt1.95 KBmikeker
#17 2371633-17-tokens-with-hyphens.patch2.62 KBmikeker
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 95,494 pass(es). View
#12 2371633-10-12.interdiff.txt644 bytesmikeker
#12 2371633-12-tokens-with-hyphens.patch2.62 KBmikeker
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 95,418 pass(es). View
#10 2371633-10-tokens-with-hyphens.patch2.66 KBmikeker
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] 95,149 pass(es), 25 fail(s), and 25 exception(s). View
#7 st_vt01.txt9.61 KBWuk
#6 t1.txt5.52 KBdawehner

Comments

mikeker’s picture

Duplicate of #2342287: Allow Twig in Views token replacement?

(I haven't look at this issue closely so not changing the status...)

Wuk’s picture

No, 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.

Wuk’s picture

I'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.

dawehner’s picture

@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.

Wuk’s picture

Yes 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

EDIT: removed view to make it easy to read rest of issue, i can upload it if needed
dawehner’s picture

FileSize
5.52 KB

Interesting, it works for me.

Wuk’s picture

FileSize
9.61 KB

I 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.

mikeker’s picture

I'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?

mikeker’s picture

Assigned: Unassigned » mikeker

*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.

mikeker’s picture

Status: Active » Needs review
FileSize
2.66 KB
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] 95,149 pass(es), 25 fail(s), and 25 exception(s). View

This 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:

  • Removed the alias to CoreXss -- unneeded
  • Rewrote things a bit to remove a double-ternary statement. Icky...

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.

Status: Needs review » Needs work

The last submitted patch, 10: 2371633-10-tokens-with-hyphens.patch, failed testing.

mikeker’s picture

Status: Needs work » Needs review
FileSize
2.62 KB
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 95,418 pass(es). View
644 bytes

Ugh... Left a debug call in the patch -- hate it when that happens! :)

mikeker’s picture

Title: Views rewrite results not rendering for most replacement patterns » Views uses hyphens in many tokens, but that is not valid Twig syntax.
Related issues: +#2487219: target_id is 0 in Entity Ref field when using formatters other than Entity ID

Closing #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.

mikeker’s picture

Issue summary: View changes
Issue tags: +Needs tests

Marking 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.

dawehner’s picture

+++ b/core/modules/views/src/Plugin/views/field/Field.php
@@ -903,7 +903,7 @@ function render_item($count, $item) {
-      $tokens['{{ ' . $this->options['id'] . '-' . $id . ' }}'] = $this->t('Raw @column', array('@column' => $id));
+      $tokens['{{ ' . $this->options['id'] . '_' . $id . ' }}'] = $this->t('Raw @column', array('@column' => $id));

Mh 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.

Cottser’s picture

Issue tags: +Twig
mikeker’s picture

FileSize
2.62 KB
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 95,494 pass(es). View
1.95 KB

#15:

I think I would use double underscore in order to remove confusion with field names including an underscore.

Good call.

And yeah indeed some form of test coverage would be neat.

Agreed. But I'm having trouble figuring out where we test field plugins for entity reference fields, for example. Any pointers?

mikeker’s picture

Assigned: mikeker » Unassigned
FileSize
3.63 KB
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 96,340 pass(es). View
1 KB

Found another use of the single-hyphen in the docblock for core/modules/views/src/Plugin/views/field/FieldPluginBase.php::addSelfTokens

mikeker’s picture

Issue summary: View changes

Updated issue summary: double-underscores instead of single.

mikeker’s picture

FileSize
3.64 KB
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 96,899 pass(es). View

Rerolled.

mikeker’s picture

Issue summary: View changes
Cottser’s picture

Status: Needs review » Needs work

This 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.

Wuk’s picture

It seems that original issue that I have reported is fixed with patch from #20.
Thank you.

guschilds’s picture

I 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!

mikeker’s picture

Status: Needs work » Needs review
Issue tags: +Novice
FileSize
4.18 KB
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 99,832 pass(es). View

Reroll. Note this should be popped back to NW after the testbot runs -- it still needs automated tests.

mikeker’s picture

Issue summary: View changes

Added novice tasks sections to the issue summary.

Cottser’s picture

Status: Needs review » Needs work
alex_drupal_dev’s picture

I added to que for retest but what should be done now on this issue?

mikeker’s picture

@alex_drupal_dev: This patch needs automated tests. See the issue summary for details.

Cottser’s picture

Gábor Hojtsy’s picture

Issue tags: -Novice

Also #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.

mikeker’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
10.66 KB
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 103,213 pass(es). View
4.73 KB
3.04 KB

I'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.

mikeker’s picture

Forgot 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.

Gábor Hojtsy’s picture

Status: Needs review » Closed (duplicate)

Closing 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!