diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 693da9a..0032770 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -497,6 +497,10 @@ public function renderVar($arg) { return NULL; } + if ($arg instanceof MarkupInterface) { + return $arg; + } + // Optimize for scalars as it is likely they come from the escape filter. if (is_scalar($arg)) { return $arg; diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php index 6bd4b8a..f7d0bcd 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -67,10 +67,9 @@ public function compile(\Twig_Compiler $compiler) { // leave as an empty array. $compiler->raw(', array('); foreach ($tokens as $token) { - $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', '); + $compiler->string($token->getAttribute('placeholder'))->raw(' => $this->env->getExtension(\'drupal_core\')->renderVar(')->subcompile($token)->raw('), '); } $compiler->raw(')'); - // Write any options passed. if (!empty($options)) { $compiler->raw(', ')->subcompile($options); diff --git a/core/modules/system/src/Tests/Theme/TwigTransTest.php b/core/modules/system/src/Tests/Theme/TwigTransTest.php index 6bfe442..e35719c 100644 --- a/core/modules/system/src/Tests/Theme/TwigTransTest.php +++ b/core/modules/system/src/Tests/Theme/TwigTransTest.php @@ -97,6 +97,14 @@ public function testTwigTransTags() { } /** + * Testing trans with render array value. + */ + public function testTransRenderArray() { + $this->drupalGet('twig-theme-test/render-array'); + $this->assertText('This is a trans render array', '{% trans %} with render array value is working correctly.'); + } + + /** * Asserts Twig trans tags. */ protected function assertTwigTransTags() { diff --git a/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php b/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php index 3ca428b..37376e6 100644 --- a/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php +++ b/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php @@ -109,4 +109,13 @@ public function renderable() { ]; } + /** + * Controller for testing trans with render array value. + */ + public function transRenderArray() { + return [ + '#theme' => 'twig_theme_test_trans_render_array', + ]; + } + } diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans_render_array.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans_render_array.html.twig new file mode 100644 index 0000000..ea04c5d --- /dev/null +++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans_render_array.html.twig @@ -0,0 +1,3 @@ +{% trans %} + This is a {{ var }}. +{% endtrans %} diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module index cba0d5b..712b8d7 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module @@ -19,6 +19,16 @@ function twig_theme_test_theme($existing, $type, $theme, $path) { 'variables' => array('var' => ''), 'template' => 'twig_theme_test.placeholder_outside_trans', ); + $items['twig_theme_test_trans_render_array'] = array( + 'variables' => array( + 'var' => array( + '#prefix' => '', + '#markup' => 'trans render array', + '#suffix' => '', + ), + ), + 'template' => 'twig_theme_test.trans_render_array', + ); $items['twig_namespace_test'] = array( 'variables' => array(), 'template' => 'twig_namespace_test', diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml index df665c3..8bc6684 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml @@ -69,3 +69,10 @@ twig_theme_test_renderable: _controller: '\Drupal\twig_theme_test\TwigThemeTestController::renderable' requirements: _access: 'TRUE' + +twig_theme_test_render_array: + path: '/twig-theme-test/render-array' + defaults: + _controller: '\Drupal\twig_theme_test\TwigThemeTestController::transRenderArray' + requirements: + _access: 'TRUE'