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
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
Comment #2
generalredneckComment #3
generalredneckOk 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.
Comment #4
generalredneckComment #6
generalredneckreversing logic... My bad
Comment #7
dawehnerI think the right thing would be to not expose
!1or%1exposed as available token, so people don't input any values which are not supported.Do you think this would make more sense.
Comment #8
generalredneckActually 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.
Comment #10
damontgomery commentedQueued this patch for testing against 8.2.0
Comment #11
realityloop commentedSimple patch that fixes the problem, running this patch successfully with 8.2.4.
Comment #12
alexpottWe 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.
Comment #13
michelle#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.
Comment #14
realityloop commented@Michelle
If you are defining a more link then tokens are possible.
Comment #15
michelleI 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?
Comment #16
generalredneckMostly 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.
Comment #17
michelleOk, 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.
Comment #19
benjamin.merkley commented#6 seemed to work for me.
Comment #20
tacituseu commentedComment #21
holist commentedConsidering 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.
Comment #22
andy_read commentedI'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?
Comment #23
tacituseu commented@Andy_Read: try #2831233: Field tokens for "historical data" fields (revisions) contain a hyphen, breaking twig templates and throwing an assertion error
Comment #24
andy_read commented@tacituseu: thanks for the reference!
Comment #28
rabithk commentedI 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
Comment #31
kasey_mk commentedThanks! #28 works for us.
Comment #34
larowlanThanks, this still needs a test.
Comment #37
victorcpereira commentedThanks! #28 works for me.
Comment #38
joaopauloscho commented#28 works for us, thanks! I'm just updating to apply it with the 9.5.9 version.
Comment #39
dewalt commentedThanks 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.
It could be fixed in many ways, like
or
Comment #40
joaopauloscho commentedAdding a new patch with the suggestions.
Comment #41
joaopauloscho commentedComment #42
smustgrave commentedWas previously tagged for tests which still needs to happen.
Also issue summary isn't super clear with steps to reproduce or proposed solution.
Comment #44
alexpottI'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.
Comment #45
alexpottHere's the new issue #3548695: Custom fields which start with a number can break views
Comment #46
smustgrave commentedSince there's been no follow up to #44 going to close out. If still an issue in D11 please re-open
Thanks all.