summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-06-26 08:50:41 (GMT)
committerAlex Pott2016-06-26 08:50:41 (GMT)
commit1dc4ba8533e5c311c6c032b2321a2722cbf0ed32 (patch)
tree4def2508f1248498a63e57abd81d1b1811e45622
parenteac023bcedcdbf79c67549e648ec4e285bce4eb4 (diff)
Issue #2698141 by TravisCarden, ChuChuNaKu, effulgentsia: Empty Twig {% trans %} tag causes unhelpful fatal exception
-rw-r--r--core/lib/Drupal/Core/Template/TwigNodeTrans.php3
-rw-r--r--core/modules/system/src/Tests/Theme/TwigTransTest.php24
2 files changed, 27 insertions, 0 deletions
diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php
index 98404b6..21006fd 100644
--- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php
+++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php
@@ -169,6 +169,9 @@ class TwigNodeTrans extends \Twig_Node {
}
}
}
+ elseif (!$body->hasAttribute('data')) {
+ throw new \Twig_Error_Syntax('{% trans %} tag cannot be empty');
+ }
else {
$text = $body->getAttribute('data');
}
diff --git a/core/modules/system/src/Tests/Theme/TwigTransTest.php b/core/modules/system/src/Tests/Theme/TwigTransTest.php
index 65d10a5..6473d6e 100644
--- a/core/modules/system/src/Tests/Theme/TwigTransTest.php
+++ b/core/modules/system/src/Tests/Theme/TwigTransTest.php
@@ -92,6 +92,30 @@ class TwigTransTest extends WebTestBase {
}
/**
+ * Test empty Twig "trans" tags.
+ */
+ public function testEmptyTwigTransTags() {
+ $elements = [
+ '#type' => 'inline_template',
+ '#template' => '{% trans %}{% endtrans %}',
+ ];
+ /** @var \Drupal\Core\Render\RendererInterface $renderer */
+ $renderer = \Drupal::service('renderer');
+
+ try {
+ $renderer->renderPlain($elements);
+
+ $this->fail('{% trans %}{% endtrans %} did not throw an exception.');
+ }
+ catch (\Twig_Error_Syntax $e) {
+ $this->assertTrue(strstr($e->getMessage(), '{% trans %} tag cannot be empty'), '{% trans %}{% endtrans %} threw the expected exception.');
+ }
+ catch (\Exception $e) {
+ $this->fail('{% trans %}{% endtrans %} threw an unexpected exception.');
+ }
+ }
+
+ /**
* Asserts Twig trans tags.
*/
protected function assertTwigTransTags() {