I created a View of content revisions. We added a field to the view (a taxonomy reference in this case) and selected the "historical data" value (to show the revision value vs the current node value). We also added an Edit link for the node.

When displaying the view we get the following warning:

Warning: assert(): Tokens need to be valid Twig variables. failed in Drupal\views\Plugin\views\PluginBase->viewsTokenReplace() (line 371 of core/modules/views/src/Plugin/views/PluginBase.php).

When debugging this, we discovered the token it is complaining about is "field_category-revision_id". This is not a valid Twig variable because of the "-". In #2548291: Views tokens need to be valid Twig variables; subtoken separator now double underscore instead of dash core changed usage of "-" to "__". However, Views still uses a "-" when adding "revision_id" to a field.

Tracked this down to core/modules/views/views.views.inc in views_field_default_views_data() (using 8.5.x core here):

    if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
      $group = $group_name;
      $field_alias = $field_name;
    }
    else {
      $group = t('@group (historical data)', ['@group' => $group_name]);
      $field_alias = $field_name . '-revision_id';
    }

This needs to be changed to "__revision_id" but not sure what else in core or contrib relies on this.

Comments

mpotter created an issue. See original summary.

OnkelTem’s picture

Confirmed. Drupal 8.5.

johnpitcairn’s picture

+1 from me. Getting this in 8.6.x whenever there is a Profile revision relationship in a commerce view. Views will check all available tokens, used or not.

As far as backward compatibility goes, I don't know that the resulting twig variable is usable anyway, is it? Let's at least see if a patch passes tests...

johnpitcairn’s picture

Title: Views created invalid field name that doesn't follow Twig token rules » Views creates invalid revision id twig token variable
johnpitcairn’s picture

Relevant change notice

johnpitcairn’s picture

Status: Active » Needs review
StatusFileSize
new548 bytes

Let's see what happens...

mpotter’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the patch! Tests are passing.

mpotter’s picture

The only problem this potentially causes is sites that already have field_name-revision_id exported in a View config will have a Broken Field after this patch and will need to update their view config and re-import. Not sure there is anything that can be done about that, but since the existing config causes the assert warning it's really already a broken field.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

We also must have missing test coverage of this functionality so we should add some to prove it works and that we don't break it in the future.

@mpotter the upgrade path is interesting. Given that it is already broken I would be inclined to not fix them BUT this gets us into an interesting situation where just saving a view will magically fix it which is also unexpected. How broken is HEAD - do you get the warning but does it still work? If that's the case we should have an upgrade path.

interx’s picture

This is addressed in https://www.drupal.org/project/drupal/issues/2831233 with a test and an upgrade path.

alexpott’s picture

Sheenam khunger’s picture

https://www.drupal.org/project/drupal/issues/2986675#comment-12753486 This patch is working fine with 8.5 .
After upgrading from 8.5.6 to 8.6.2 , this patch do not work as line number changed in the core . It broke the fields in views as its changed the field to field-name-revision_id.
broken-view

views-field-revision-twig-tokens-2986675-12-D8.patch , changes done here according to the Drupal 8.6 .

jessey’s picture

Version: 8.7.x-dev » 10.0.x-dev
StatusFileSize
new552 bytes