diff --git a/core/modules/system/src/Tests/Theme/EngineTwigTest.php b/core/modules/system/src/Tests/Theme/EngineTwigTest.php index 02139ab..c6139ed 100644 --- a/core/modules/system/src/Tests/Theme/EngineTwigTest.php +++ b/core/modules/system/src/Tests/Theme/EngineTwigTest.php @@ -103,7 +103,7 @@ public function testTwigUrlToString() { } /** - * Tests the file_url Twig functions. + * Tests the automatic/magic calling of toString() on objects, if exists. */ public function testTwigFileUrls() { $this->drupalGet('/twig-theme-test/file-url'); diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine index 46646d9..db71af5 100644 --- a/core/themes/engines/twig/twig.engine +++ b/core/themes/engines/twig/twig.engine @@ -150,6 +150,9 @@ function twig_render_var($arg) { if (method_exists($arg, '__toString')) { return (string) $arg; } + // You can't throw exceptions in the magic PHP __toString methods, see + // http://php.net/manual/en/language.oop5.magic.php#object.tostring so + // we also support a toString method. elseif (method_exists($arg, 'toString')) { return $arg->toString(); } @@ -240,6 +243,9 @@ function twig_drupal_escape_filter(\Twig_Environment $env, $string, $strategy = if (method_exists($string, '__toString')) { $return = (string) $string; } + // You can't throw exceptions in the magic PHP __toString methods, see + // http://php.net/manual/en/language.oop5.magic.php#object.tostring so + // we also support a toString method. elseif (method_exists($string, 'toString')) { $return = $string->toString(); }