diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php index 264a511172..91304f10e7 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -62,7 +62,7 @@ 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(')'); diff --git a/core/modules/system/src/Tests/Theme/TwigTransTest.php b/core/modules/system/src/Tests/Theme/TwigTransTest.php index b73cdaf0c2..7d0402877d 100644 --- a/core/modules/system/src/Tests/Theme/TwigTransTest.php +++ b/core/modules/system/src/Tests/Theme/TwigTransTest.php @@ -116,6 +116,14 @@ public function testEmptyTwigTransTags() { } /** + * 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 8e1f69e792..6c346906e4 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 @@ -104,4 +104,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 0000000000..ea04c5d477 --- /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 a8b4086203..6e82a0295b 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 @@ -28,6 +28,16 @@ function twig_theme_test_theme($existing, $type, $theme, $path) { 'variables' => [], 'template' => 'twig_namespace_test', ]; + $items['twig_theme_test_trans_render_array'] = [ + 'variables' => [ + 'var' => [ + '#prefix' => '', + '#markup' => 'trans render array', + '#suffix' => '', + ], + ], + 'template' => 'twig_theme_test.trans_render_array', + ]; $items['twig_registry_loader_test'] = [ 'variables' => [], ]; 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 df665c3464..8bc66848aa 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' diff --git a/core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php b/core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php index 5e3bc722e3..bf79d69c7d 100644 --- a/core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php +++ b/core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\system\Functional\Theme; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Tests\BrowserTestBase; /** @@ -86,4 +87,14 @@ public function testsRenderZeroValue() { $this->assertIdentical($extension->renderVar(0.0), 0, 'TwigExtension::renderVar() renders zero correctly when provided as a double.'); } + /** + * Tests output of MarkupInterface of TwigExtension->renderVar(). + */ + public function testRenderMarkup() { + /** @var \Drupal\Core\Template\TwigExtension $extension */ + $extension = \Drupal::service('twig.extension'); + $markup = new FormattableMarkup('This is a @markup', ['@markup' => 'MarkupInterface']); + $this->assertIdentical($extension->renderVar($markup), 'This is a MarkupInterface', 'TwigExtension::renderVar() returns MarkupInterface correctly.'); + } + }