diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 735a434..306d49f 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -116,6 +116,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']), ]; } @@ -274,6 +275,16 @@ 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. @@ -364,6 +375,8 @@ public function escapePlaceholder($env, $string) { * * @return string|null * The escaped, rendered output, or NULL if there is no valid output. + * + * @throws \Exception */ public function escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE) { // Check for a numeric zero int or float. @@ -447,6 +460,8 @@ public function escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $ * * @see render * @see TwigNodeVisitor + * + * @throws \Exception */ public function renderVar($arg) { // Check for a numeric zero int or float. diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php index 3e78e91..a873b5f 100644 --- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php @@ -104,6 +104,33 @@ public function testActiveTheme() { } /** + * 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); + } + + /** * Tests the escaping of objects implementing SafeStringInterface. * * @covers ::escapeFilter @@ -190,5 +217,4 @@ public function __construct($string) { public function __toString() { return $this->string; } - }