diff --git a/core/modules/views/src/Plugin/views/PluginBase.php b/core/modules/views/src/Plugin/views/PluginBase.php index c876f6db62e..7b11a31c175 100644 --- a/core/modules/views/src/Plugin/views/PluginBase.php +++ b/core/modules/views/src/Plugin/views/PluginBase.php @@ -373,7 +373,8 @@ abstract class PluginBase extends ComponentPluginBase implements ContainerFactor assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/\', $top) === 1', 'Tokens need to be valid Twig variables.'); $token_array = array(array_pop($parts) => $replacement); foreach (array_reverse($parts) as $key) { - assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/\', $key) === 1', 'Tokens need to be valid Twig variables.'); + // The key could also be numeric (array index) so allow that. + 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.'); $token_array = array($key => $token_array); } if (!isset($twig_tokens[$top])) { diff --git a/core/modules/views/src/Tests/Plugin/PluginBaseTest.php b/core/modules/views/src/Tests/Plugin/PluginBaseTest.php index c9ba78fc14d..a70082cfeeb 100644 --- a/core/modules/views/src/Tests/Plugin/PluginBaseTest.php +++ b/core/modules/views/src/Tests/Plugin/PluginBaseTest.php @@ -55,6 +55,16 @@ class PluginBaseTest extends KernelTestBase { }); $this->assertIdentical($result, 'first comes before second'); + + // Test tokens with numeric indexes. + $text = '{{ argument.0.first }} comes before {{ argument.1.second }}'; + $tokens = ['{{ argument.0.first }}' => 'first', '{{ argument.1.second }}' => 'second']; + + $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) { + return $this->testPluginBase->viewsTokenReplace($text, $tokens); + }); + + $this->assertIdentical($result, 'first comes before second'); } /**