diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 01a3d1a..caf8233c 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -504,7 +504,7 @@ public function renderVar($arg) { } // Optimize for scalars as it is likely they come from the escape filter. - if (is_scalar($arg)) { + if (is_scalar($arg) || $arg instanceof MarkupInterface || $arg instanceof \Twig_Markup) { return $arg; } diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php index b2cffa6..0eecf1a 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -67,7 +67,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/TwigExtensionTest.php b/core/modules/system/src/Tests/Theme/TwigExtensionTest.php index 7ca45fb..88acb6b 100644 --- a/core/modules/system/src/Tests/Theme/TwigExtensionTest.php +++ b/core/modules/system/src/Tests/Theme/TwigExtensionTest.php @@ -8,6 +8,7 @@ namespace Drupal\system\Tests\Theme; use Drupal\simpletest\WebTestBase; +use Drupal\Component\Render\FormattableMarkup; /** * Tests Twig extensions. @@ -91,4 +92,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), $markup, 'TwigExtension::renderVar() returns MarkupInterface correctly.'); + } + } diff --git a/core/modules/system/src/Tests/Theme/TwigTransTest.php b/core/modules/system/src/Tests/Theme/TwigTransTest.php index 9eec247..423b49e 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 0d6bad8..91afc8a 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 @@ -24,6 +24,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'