diff --git a/core/core.services.yml b/core/core.services.yml index 53a2725..c350845 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1309,8 +1309,9 @@ services: tags: - { name: twig.extension, priority: 100 } calls: - - [setGenerators, ['@url_generator']] + - [setUrlGenerator, ['@url_generator']] - [setLinkGenerator, ['@link_generator']] + - [setThemeManager, ['@theme.manager']] # @todo Figure out what to do about debugging functions. # @see http://drupal.org/node/1804998 twig.extension.debug: diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 44b0a2a..aefab4f 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -16,6 +16,7 @@ use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Url; use Drupal\Core\Utility\LinkGeneratorInterface; +use Drupal\Core\Theme\ThemeManagerInterface; /** * A class providing Drupal Twig extensions. @@ -48,6 +49,13 @@ class TwigExtension extends \Twig_Extension { protected $renderer; /** + * The theme manager. + * + * @var \Drupal\Core\Theme\ThemeManagerInterface + */ + protected $themeManager; + + /** * Constructs \Drupal\Core\Template\TwigExtension. * * @param \Drupal\Core\Render\RendererInterface $renderer @@ -65,7 +73,7 @@ public function __construct(RendererInterface $renderer) { * * @return $this */ - public function setGenerators(UrlGeneratorInterface $url_generator) { + public function setUrlGenerator(UrlGeneratorInterface $url_generator) { $this->urlGenerator = $url_generator; return $this; } @@ -84,6 +92,19 @@ public function setLinkGenerator(LinkGeneratorInterface $link_generator) { } /** + * Sets the theme manager. + * + * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager + * The theme manager. + * + * @return $this + */ + public function setThemeManager(ThemeManagerInterface $theme_manager) { + $this->themeManager = $theme_manager; + return $this; + } + + /** * {@inheritdoc} */ public function getFunctions() { @@ -97,7 +118,8 @@ public function getFunctions() { new \Twig_SimpleFunction('url_from_path', array($this, 'getUrlFromPath'), array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))), new \Twig_SimpleFunction('link', array($this, 'getLink')), new \Twig_SimpleFunction('file_url', 'file_create_url'), - new \Twig_SimpleFunction('attach_library', array($this, 'attachLibrary')) + new \Twig_SimpleFunction('attach_library', array($this, 'attachLibrary')), + new \Twig_SimpleFunction('active_theme', array($this, 'activeTheme')) ); } @@ -242,6 +264,16 @@ public function getLink($text, $url) { } /** + * Gets the name of the active theme. + * + * @return string + * The name of the active theme. + */ + public function activeTheme() { + return $this->themeManager->getActiveTheme()->getName(); + } + + /** * Determines at compile time whether the generated URL will be safe. * * Saves the unneeded automatic escaping for performance reasons. diff --git a/core/modules/system/src/Tests/Theme/EngineTwigTest.php b/core/modules/system/src/Tests/Theme/EngineTwigTest.php index ff84af7..449bf03 100644 --- a/core/modules/system/src/Tests/Theme/EngineTwigTest.php +++ b/core/modules/system/src/Tests/Theme/EngineTwigTest.php @@ -112,6 +112,15 @@ public function testTwigFileUrls() { } /** + * Tests the active_theme() function. + */ + public function testActiveTheme() { + $this->drupalGet('/twig-theme-test/active_theme'); + $theme = \Drupal::service('theme.manager')->getActiveTheme()->getName(); + $this->assertRaw('