diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 23091d5..bdcfc9c 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -137,6 +137,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']), ]; } @@ -295,6 +296,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. @@ -361,6 +372,8 @@ public function attachLibrary($library) { * * @return string|null * The escaped, rendered output, or NULL if there is no valid output. + * + * @throws \Exception */ public function escapePlaceholder($env, $string) { return '' . $this->escapeFilter($env, $string) . ''; @@ -474,6 +487,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 01c1bc5..e7fa8fb 100644 --- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php @@ -105,6 +105,7 @@ public function testActiveTheme() { } /** +<<<<<<< HEAD * Tests the format_date filter. */ public function testFormatDate() { @@ -126,6 +127,33 @@ public function testFormatDate() { } /** + * 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 MarkupInterface. * * @covers ::escapeFilter