I've come across the Exception below. It seems this happens when Views is trying to figure out %1 !1, %2, or !2. This this case... I have 2 contextual filters. This only happens if I set the "More Link" to the following settings:

More Link: Yes
[X] Create More Link
[X] Always display more link
Link Display: Custom
[ ] None
[X] Custom URL
Custom URL: "/somepage"

All the tokens sent to view in this case are the following:

'{{ arguments.null }}' => string(2) "63"
'{{ raw_arguments.null }}' => string(2) "63"
'{{ arguments.field_degree_program_target_id }}' => string(4) "6605"
'{{ raw_arguments.field_degree_program_target_id }}' => string(4) "6605"
'%1' => ''
'!1' => string(2) "63"
'%2' => ''
'!2' => string(4) "6605"

The expected results would be to have the correct variable name for the replacement so that this validation will work.... or to change the validation where it's not a "hard Exception", but something that will fail softly.

AssertionError: Tokens need to be valid Twig variables. in Drupal\Component\Assertion\Handle::Drupal\Component\Assertion\{closure}() (line 367 of core/modules/views/src/Plugin/views/PluginBase.php).
assert('preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $token) === 1', 'Tokens need to be valid Twig variables.') (Line: 367)
Drupal\views\Plugin\views\PluginBase->viewsTokenReplace('/faculty', Array) (Line: 2080)
Drupal\views\Plugin\views\display\DisplayPluginBase->renderMoreLink() (Line: 2164)
Drupal\views\Plugin\views\display\DisplayPluginBase->elementPreRender(Array)
call_user_func(Array, Array) (Line: 381)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 448)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 468)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 84)
__TwigTemplate_b30d5878476cc595a2585c141ab5c83b010a9e8831025f3b5bfa0e1e9e2289df->block_content(Array, Array) (Line: 167)
Twig_Template->displayBlock('content', Array, Array) (Line: 73)
__TwigTemplate_b30d5878476cc595a2585c141ab5c83b010a9e8831025f3b5bfa0e1e9e2289df->doDisplay(Array, Array) (Line: 387)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('themes/custom/customtheme/templates/blocks/block.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('block', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 448)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 468)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 107)
__TwigTemplate_740a486138a3a9fc024c114124f062223ca63d89b3006208489a492523efb875->doDisplay(Array, Array) (Line: 387)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('themes/custom/customtheme/templates/page.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('page', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 468)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 92)
__TwigTemplate_c9343766bf6adc2d3ee69a1ad1d87d0f76608c2a7ce17bb3255275241b160944->doDisplay(Array, Array) (Line: 387)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('themes/custom/customtheme/templates/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Issue fork drupal-2780891

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

generalredneck created an issue. See original summary.

generalredneck’s picture

Issue summary: View changes
generalredneck’s picture

Status: Active » Needs review
StatusFileSize
new1.92 KB

Ok so this fix probably isn't correct. However, it gets me moving because I don't use any arguments in the more link. A more correct solution (probably in getArgumentsTokens) would probably warranted.

generalredneck’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 3: views-ignore_invalid_tokens_in_replace-2780891-3-D8.1.x.patch, failed testing.

generalredneck’s picture

Status: Needs work » Needs review
StatusFileSize
new1.92 KB

reversing logic... My bad

dawehner’s picture

Issue tags: +Drupalaton

I think the right thing would be to not expose !1 or %1 exposed as available token, so people don't input any values which are not supported.
Do you think this would make more sense.

generalredneck’s picture

Actually that should be a valid route given that you have tokens for the fields. Good call.. in that case there should be a change in Drupal\views\Plugin\views\display\DisplayPluginBase->getArgtumentsTokens to remove those... though at the same time I still don't agree that we should fatal on a bad token.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

damontgomery’s picture

Queued this patch for testing against 8.2.0

realityloop’s picture

Status: Needs review » Reviewed & tested by the community

Simple patch that fixes the problem, running this patch successfully with 8.2.4.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests
+++ b/core/modules/views/src/Plugin/views/PluginBase.php
@@ -364,17 +364,26 @@ protected function viewsTokenReplace($text, $tokens) {
-        assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/\', $token) === 1', 'Tokens need to be valid Twig variables.');
+        if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $token) !== 1) {
+          // Tokens need to be valid Twig variables.
+          continue;
+        }
...
-        assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/\', $top) === 1', 'Tokens need to be valid Twig variables.');
+        if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $top) !== 1) {
+          // Tokens need to be valid Twig variables.
+          continue;
+        }
...
-          assert('is_numeric($key) || (preg_match(\'/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/\', $key) === 1)', 'Tokens need to be valid Twig variables.');
+          if (!is_numeric($key) && preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key) !== 1) {
+            // Tokens need to be valid Twig variables.
+            continue;
+          }

We shouldn't remove the assertions since these log errors these but maybe we should do them differently in light of this. And this fix should be tested.

michelle’s picture

#6 solved the problem for me. My more link doesn't have tokens so I don't understand why it even tries to do any token replacement.

realityloop’s picture

@Michelle

If you are defining a more link then tokens are possible.

michelle’s picture

I realize they are _possible_ but there aren't any actually there. If there aren't any tokens to replace, why is it trying to use the %1 as a token in the more link when the custom URL is just straight text?

generalredneck’s picture

Mostly because it's easier and more performent to just replace tokens than try to check to see if there are tokens then replace. Therefore any field/area where tokens can replaced are typically run through the token replacement code regardless of they actually have tokens in them or not.

Keep in mind that %1 isn't the only token. There are an unknown number of tokens to replace.

michelle’s picture

Ok, thanks. I mentioned %1 because that and another one that was a symbol before a 1 (forget which symbol) were the two that it was choking on.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

benjamin.merkley’s picture

#6 seemed to work for me.

holist’s picture

Status: Needs work » Needs review
StatusFileSize
new819 bytes

Considering comments by @alexpott in #12, I'd like to throw in this fairly straight-forward solution of filtering out the contextual filter tokens from Twig tokens, as the whole point of the assert checks is that they wouldn't be valid.

Haven't thought this out completely but for me this is a serious bug and I need a solution so I'm going with this one for now.

andy_read’s picture

I've just hit this error, but the root cause seems to be slightly different: my view is working with revisions and I get the error when I add a taxonomy reference field before the text field that is being parsed for tokens.

Specifically, the taxonomy field seems to generate a token named field_my_term-revision_id, i.e. it contains a hyphen, which the regex doesn't allow. Should the taxonomy/revision code be fixed to not use '-' as glue, or should this be added to the regex?

andy_read’s picture

@tacituseu: thanks for the reference!

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rabithk’s picture

I have the same error for the core version 8.6.16, but the #21 alone not helping this, but we can modify the #6.
Adding a new patch for 8.6.16

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kasey_mk’s picture

Thanks! #28 works for us.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
larowlan’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative

Thanks, this still needs a test.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

victorcpereira’s picture

Thanks! #28 works for me.

joaopauloscho’s picture

#28 works for us, thanks! I'm just updating to apply it with the 9.5.9 version.

dewalt’s picture

Thanks for patch, generally it works, but I'd like to propose some improvements.

1. The last "continue" is in one more foreach, so I think there should be
continue 2;

2. "array_pop" removes the last element in the array, and it isn't validated in further foreach cycle.

$token_array = [array_pop($parts) => $replacement];

It could be fixed in many ways, like

// Its isn't good from design perspective as variable changes type from sting to array, but works in PHP.
$token_array = $replacement;

or

$token_array = NULL;
// ...
$token_array = [$key => $token_array ?? $replacement];
joaopauloscho’s picture

Adding a new patch with the suggestions.

joaopauloscho’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Was previously tagged for tests which still needs to happen.

Also issue summary isn't super clear with steps to reproduce or proposed solution.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alexpott’s picture

Status: Needs work » Postponed (maintainer needs more info)

I've spent sometime trying to reproduce this. I've found an issue but it not related to the issue summary explanation of this problem. You get the same error if you have any custom fields that start with a number. Note that this is difficult to do with Drupal via the UI because by default Drupal will prefix field names with field_ but it is possible to alter an entity type to add a base field like '1_something' and then Views will trigger this assertion message if you use views and use the field rewriting feature. I'm going to open a new issue for this specific case and I'm going to postpone this one and ask for more information. The new issue will update the assertion messages to be more helpful and tell you which token is invalid.

I've grepped core and a lot of contrib and I cannot find anything that is causing the old tokens of %1 and !1 etc... to still be used.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Since there's been no follow up to #44 going to close out. If still an issue in D11 please re-open

Thanks all.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.