diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php index c64956cfcd..34df39f099 100644 --- a/core/lib/Drupal/Core/Template/TwigEnvironment.php +++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php @@ -13,6 +13,7 @@ use Twig\Error\SyntaxError; use Twig\Extension\SandboxExtension; use Twig\Loader\LoaderInterface; +use Twig\Source; /** * A class that defines a Twig environment for Drupal. @@ -104,13 +105,7 @@ public function __construct($root, CacheBackendInterface $cache, $twig_extension /** * {@inheritdoc} */ - public function compileSource($source, $name = NULL) { - if (!$source instanceof \Twig_Source) { - // @codingStandardsIgnoreLine (Twig deprecation). - @trigger_error(sprintf('Passing a string as the $source argument of %s() is deprecated since version 1.27. Pass a Twig\Source instance instead.', __METHOD__), E_USER_DEPRECATED); - $source = new \Twig_Source($source, $name); - } - + public function compileSource(Source $source) { // Note: always use \Drupal\Core\Serialization\Yaml here instead of the // "serializer.yaml" service. This allows the core serializer to utilize // core related functionality which isn't available as the standalone @@ -124,7 +119,7 @@ public function compileSource($source, $name = NULL) { try { if (($line = $frontMatter->getLine()) > 1) { $content = "{% line $line %}" . $frontMatter->getContent(); - $source = new \Twig_Source($content, $source->getName(), $source->getPath()); + $source = new Source($content, $source->getName(), $source->getPath()); } } catch (FrontMatterParseException $exception) { @@ -134,7 +129,7 @@ public function compileSource($source, $name = NULL) { throw new SyntaxError($message, $exception->getSourceLine(), $source, $exception); } - return parent::compileSource($source, $name); + return parent::compileSource($source); } /** @@ -169,13 +164,10 @@ public function getTwigCachePrefix() { * * @throws \Twig\Error\LoaderError * @throws \Twig\Error\SyntaxError - * - * @todo Simplify this method to just use $loader->getSourceContext($name). - * @see https://www.drupal.org/project/drupal/issues/3041076 */ public function getTemplateMetadata(string $name): array { $loader = $this->getLoader(); - $source = $loader instanceof \Twig_SourceContextLoaderInterface ? $loader->getSourceContext($name) : new \Twig_Source($loader->getSource($name), $name); + $source = $loader->getSourceContext($name); // Note: always use \Drupal\Core\Serialization\Yaml here instead of the // "serializer.yaml" service. This allows the core serializer to utilize diff --git a/core/tests/Drupal/KernelTests/Core/Theme/FrontMatterTest.php b/core/tests/Drupal/KernelTests/Core/Theme/FrontMatterTest.php index 0f857dba98..f902547dce 100644 --- a/core/tests/Drupal/KernelTests/Core/Theme/FrontMatterTest.php +++ b/core/tests/Drupal/KernelTests/Core/Theme/FrontMatterTest.php @@ -70,10 +70,10 @@ protected function createTwigTemplate(string $content = ''): string { * @covers \Drupal\Component\FrontMatter\Exception\FrontMatterParseException */ public function testFrontMatterBroken() { - $source = "---\nbroken: {\n---\n" . ComponentFrontMatterTest::SOURCE; + $source = "---\ncollection:\n- key: foo\n foo: bar\n---\n" . ComponentFrontMatterTest::SOURCE; $file = $this->createTwigTemplate($source); $this->expectException(SyntaxError::class); - $this->expectExceptionMessage('An error occurred when attempting to parse front matter data on line 3 in ' . $file); + $this->expectExceptionMessage('An error occurred when attempting to parse front matter data on line 4 in ' . $file); $this->twig->getTemplateMetadata(basename($file)); } diff --git a/core/tests/Drupal/Tests/Component/FrontMatter/FrontMatterTest.php b/core/tests/Drupal/Tests/Component/FrontMatter/FrontMatterTest.php index 47de2bf177..3e6a27aabf 100644 --- a/core/tests/Drupal/Tests/Component/FrontMatter/FrontMatterTest.php +++ b/core/tests/Drupal/Tests/Component/FrontMatter/FrontMatterTest.php @@ -64,8 +64,8 @@ public function testFrontMatterSerializerException() { */ public function testFrontMatterBroken() { $this->expectException(FrontMatterParseException::class); - $this->expectExceptionMessage('An error occurred when attempting to parse front matter data on line 3'); - $source = "---\nbroken: {\n---\n"; + $this->expectExceptionMessage('An error occurred when attempting to parse front matter data on line 4'); + $source = "---\ncollection:\n- key: foo\n foo: bar\n---\n"; FrontMatter::create($source)->getData(); }