Field tokens for "historical data" fields (revisions) contain a hyphen, breaking twig templates and throwing an assertion error

Created on 28 November 2016, almost 8 years ago
Updated 17 November 2023, 12 months ago

Problem/Motivation

This is easily reproducible on simplytest.me:

1. Install Drupal w/ standard profile
2. Add an article node with garbage info
3. Create a view with base table of Content Revisions, displaying fields.
4. Add field "Tags" under category "Content (historical data)"
5. In settings for that field, rewrite the field using "Override the output of this field with custom text" (actually doesn't matter what you pick, you just need something that exposes the replacement tokens)
6. Observe that the replacement token for that field contains a hyphen: "{{ field_tags-revision_id__target_id }}"

Before


The problem is that you cannot have hyphens in twig variables like that. I believe it should be two underscores instead based on what I've seen in some other issues.

Proposed resolution

- Use the field alias instead, but don't break things that use field name in the view.
- Views post-update that is run as batch to be scalable for sites with many views.
- Write a test to assert the problem.
- Write a test to assert the post-update.

After


Remaining tasks

- . Done in #52, tested in #58.
- , tested in #58
- , done in #58.
- . Done in #65

Completed

- Rewrote views post-update to use Batch API and added a test for views post-update.
- Patch #25 🐛 Field tokens for "historical data" fields (revisions) contain a hyphen, breaking twig templates and throwing an assertion error Fixed with views post-update, fix and test to assert the issue.
- Manual testing (DONE): Add Before/After screenshots.

Original report by bkosborne

This causes a couple issues:

1. There's an assertion in PluginBase::viewsTokenReplace() that checks if tokens are valid twig variables. So if the token replacement is performed for whatever reason, this assertion will fail and cause a 500 error.
2. I believe this would also prevent twig from performing the replacement correctly and/or using the proper field template for the field, but I can't be sure because I didn't test that behavior. The replacement token replaces the string 0 or 1, but not the actual value for the field.

I did a little big of digging and see that the token name for a given field is literally the "id" of the field, see FieldPluginBase::getFieldTokenPlaceholder(). Not sure on the solution here since I'm completely unfamiliar with this code.

🐛 Bug report
Status

Fixed

Version

10.1

Component
Views 

Last updated about 20 hours ago

Created by

🇺🇸United States bkosborne New Jersey, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France nicolas bouteille

    With patch #152 applied on 9.4.8, I get an error when I try to save my view.
    ParseError: syntax error, unexpected ')' in Composer\Autoload\includeFile() (line 587 of core/modules/views/src/ViewsConfigUpdater.php).
    After looking at the code, it seems the foreach line 587 is indeed incomplete:
    foreach ($displays) {
    Also, the new tokens don't seem to be available...
    Patch #151 does bring the new tokens which works great thanks and does not prevent saving the view.

  • Status changed to Needs review almost 2 years ago
  • 🇺🇸United States mradcliffe USA

    I redid the re-roll of the patch from #146 and then re-added the tests in #139.

  • 🇮🇳India _utsavsharma

    Fixed CCF for #156.

  • 🇦🇺Australia acbramley

    Tests are back as of a few patches ago.

    1. Fixed the initial test fail by adding a UUID to the fixture yaml.
    2. Fixed the actual failure by referencing $display by reference.
    3. Fixed the doc block for processRevisionFieldHyphenFix
    4. Added various return types, etc
  • 🇳🇱Netherlands Lendude Amsterdam

    So to get back to my point in #82, we still miss a proper test-only patch that proves we are fixing a bug here.

    So here is my take on that, changed the test view to use the old replacement token en matched the test to use that token. As expected it fails, but now we have proof.

    Also uploading the patch from #159 again to make the testbot happy and it won't get stuck on the test-only patch

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    Confirmed the issue following the steps in the IS.
    Applied patch #160 which solves the issue
    Also patch TEST_ONLY-160 shows the issue.

    Think this will need a change record to announce the new public function processRevisionFieldHyphenFix

  • Status changed to RTBC over 1 year ago
  • 🇺🇸United States smustgrave

    Another random failure

  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    Updating issue credits

    • larowlan committed 246d721a on 10.1.x
      Issue #2831233 by mradcliffe, philltran, vsujeetkumar, selwynpolit, s....
  • Status changed to Fixed over 1 year ago
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    Committed to 10.1.x

    Not backporting because of update path

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • 🇨🇦Canada liquidcms

    I know this is closed and I am late to the game here; but do I have this right?

    In D10 historical fields in views were changed from __revision_id to -revision_id. There was no updb script for this, so when i migrated to D10 a bunch of views now have broken/missing handlers. And then, once i go through and fix all of these by re-adding all these fields, all the rewrites are broken because twig can't handle the new field values in its tokens?

    And the fix here, is to preprocess the twig token values and put them back to what they used to be (D9)? Silly question, but wouldnt it have been easier (and not broken a ton of sites) to just change the field ids back to what they were?

    That being said, anyone know if any of these patches apply to D10.0? Haven't found one yet. :(

  • Status changed to Fixed 12 months ago
  • 🇨🇦Canada liquidcms

    No, it does seem like this patch is putting the views field ids back as they were in D9 __revision_id. Yay!

    Couldn't find a patch here that worked with 10.0.11 so attaching one here. It is based off the patch from #168.

Production build 0.71.5 2024