diff --git a/core/core.services.yml b/core/core.services.yml index 95177ff..424ea51 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1227,7 +1227,7 @@ services: tags: - { name: twig.loader, priority: 0 } twig.loader.string: - class: Twig_Loader_String + class: Drupal\Core\Template\Loader\StringLoader tags: - { name: twig.loader, priority: -100 } element_info: diff --git a/core/lib/Drupal/Core/Template/Loader/StringLoader.php b/core/lib/Drupal/Core/Template/Loader/StringLoader.php new file mode 100644 index 0000000..d24b9bf --- /dev/null +++ b/core/lib/Drupal/Core/Template/Loader/StringLoader.php @@ -0,0 +1,42 @@ +loadTemplate($template_string, NULL)->render($context); } diff --git a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php index 8edb4b2..b2e18d9 100644 --- a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php +++ b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php @@ -65,5 +65,21 @@ public function testInlineTemplate() { $this->assertEqual(drupal_render($element_copy), 'test-with-context muuh'); } + /** + * Tests that exceptions are thrown when a template is not found. + */ + public function testTemplateNotFoundException() { + /** @var \Drupal\Core\Template\TwigEnvironment $environment */ + $environment = \Drupal::service('twig'); + + try { + $environment->loadTemplate('this-template-does-not-exist.html.twig')->render(array()); + $this->fail('Did not throw an exception as expected.'); + } + catch (\Twig_Error_Loader $e) { + $this->assertTrue(strpos($e->getMessage(), 'Template "this-template-does-not-exist.html.twig" is not defined') === 0); + } + } + }