Problem/Motivation
If assert.active is enabled (default PHP behaviour), and you open a view with non-standard parameters, Views will add the invalid parameters as twig tokens, and will fail the assertion later down the line, and throw the following exception:
The website encountered an unexpected error. Please try again later.
AssertionError: Tokens need to be valid Twig variables. in assert() (line 390 of core/modules/views/src/Plugin/views/PluginBase.php).
Drupal\views\Plugin\views\PluginBase->viewsTokenReplace('<a href="{{ view_node }}">{{ type }}</a>', Array) (Line: 1396)
Drupal\views\Plugin\views\field\FieldPluginBase->renderAltered(Array, Array) (Line: 1289)
Drupal\views\Plugin\views\field\FieldPluginBase->renderText(Array) (Line: 1243)
Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object) (Line: 238)
template_preprocess_views_view_field(Array, 'views_view_field', Array)
call_user_func_array('template_preprocess_views_view_field', Array) (Line: 285)
Drupal\Core\Theme\ThemeManager->render('views_view_field', Array) (Line: 433)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 1788)
Drupal\views\Plugin\views\field\FieldPluginBase->theme(Object) (Line: 758)
Drupal\views\Plugin\views\style\StylePluginBase->elementPreRenderRow(Array)
We should only add valid twig tokens in the list.
Steps to reproduce
PHP assertions might be on by default, but if they aren't, you may enforce it by adding the following to your local settings.php:
assert_options(ASSERT_ACTIVE, TRUE);
\Drupal\Component\Assertion\Handle::register();
Attached a copy of a sample
views.view.list_of_users.yml
view config to replicate the issue.
- Create a view which includes a field which incorporates token rewrites
- Add the following query string to the view's URL:
?amp;page=0&category[64]=64&page=0 - Views will attempt to add invalid tokens such as:
{{ arguments.amp;page }}into the list and subsequently fail its assertions
Proposed resolution
Validate that the query string parameters injected are actually valid twig variable tokens, skipping them otherwise.
Or alternatively detect those non-standard parameters, and replace with {{ arguments['amp;page'] }} type syntax.
Remaining tasks
Provide issue fork/patch.
User interface changes
N/A
API changes
N/A
| Comment | File | Size | Author |
|---|
Issue fork drupal-3414144
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:
- 3414144-invalid-twig-token
changes, plain diff MR !6144
Comments
Comment #2
codebymikey commentedAttached a patch which stops invalid query parameters from being added in.
I also updated the documentation for the
\Drupal\views\Plugin\views\field\FieldPluginBase::getTokenValuesRecursivefunction.Comment #4
smustgrave commentedMR seems to have failures,
FYI don't need to upload test-only patches, MRs have a test-only feature run.
Left some nitpicky comments also.
Comment #5
codebymikey commentedWasn't aware of the test-only pipelines, I've updated the coding standard issues and tests seem to pass.
Comment #6
codebymikey commentedComment #7
smustgrave commentedAppears you ran the test-only feature so posting results here for quick glance.
Issue summary appears complete.
Change seems small enough with the additional check that I can mark it now.
Comment #10
catchCommitted/pushed to 11.x and cherry-picked to 10.2.x, thanks!