diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 2b76b20..a09160d 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -99,6 +99,7 @@ public function getFunctions() { new \Twig_SimpleFunction('link', array($this, 'getLink')), new \Twig_SimpleFunction('file_url', 'file_create_url'), new \Twig_SimpleFunction('attach_library', [$this, 'attachLibrary']), + new \Twig_SimpleFunction('active_theme_path', [$this, 'getActiveThemePath']), new \Twig_SimpleFunction('active_theme', [$this, 'getActiveTheme']), ]; } @@ -279,6 +280,17 @@ public function getActiveTheme() { } /** + * Gets the path of the active theme. + * + * @return string + * The path to the active theme. + */ + public function getActiveThemePath() { + return $this->themeManager->getActiveTheme()->getPath(); + + } + + /** * Determines at compile time whether the generated URL will be safe. * * Saves the unneeded automatic escaping for performance reasons. diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php index ed82e38..166a6f5 100644 --- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php @@ -99,4 +99,31 @@ public function testActiveTheme() { $this->assertEquals('test_theme', $result); } + /** + * Tests the active_theme_path function. + */ + public function testActiveThemePath() { + $renderer = $this->getMock('\Drupal\Core\Render\RendererInterface'); + $extension = new TwigExtension($renderer); + $theme_manager = $this->getMock('\Drupal\Core\Theme\ThemeManagerInterface'); + $active_theme = $this->getMockBuilder('\Drupal\Core\Theme\ActiveTheme') + ->disableOriginalConstructor() + ->getMock(); + $active_theme + ->expects($this->once()) + ->method('getPath') + ->willReturn('foo/bar'); + $theme_manager + ->expects($this->once()) + ->method('getActiveTheme') + ->willReturn($active_theme); + $extension->setThemeManager($theme_manager); + + $loader = new \Twig_Loader_String(); + $twig = new \Twig_Environment($loader); + $twig->addExtension($extension); + $result = $twig->render('{{ active_theme_path() }}'); + $this->assertEquals('foo/bar', $result); + } + }