Commit ffe9803c authored by alexpott's avatar alexpott

Issue #2416831 by cilefen, vasi, tadityar, Noe_, lauriii, akalata, JeroenT,...

Issue #2416831 by cilefen, vasi, tadityar, Noe_, lauriii, akalata, JeroenT, Cottser, davidhernandez: Add an active_theme twig function
parent e96d4270
......@@ -1323,6 +1323,7 @@ services:
- { name: twig.extension, priority: 100 }
calls:
- [setGenerators, ['@url_generator']]
- [setThemeManager, ['@theme.manager']]
# @todo Figure out what to do about debugging functions.
# @see https://www.drupal.org/node/1804998
twig.extension.debug:
......
......@@ -15,6 +15,7 @@
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Url;
/**
......@@ -40,6 +41,13 @@ class TwigExtension extends \Twig_Extension {
*/
protected $renderer;
/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* Constructs \Drupal\Core\Template\TwigExtension.
*
......@@ -63,11 +71,24 @@ public function setGenerators(UrlGeneratorInterface $url_generator) {
return $this;
}
/**
* 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() {
return array(
return [
// This function will receive a renderable array, if an array is detected.
new \Twig_SimpleFunction('render_var', array($this, 'renderVar')),
// The url and path function are defined in close parallel to those found
......@@ -77,8 +98,9 @@ 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', [$this, 'attachLibrary']),
new \Twig_SimpleFunction('active_theme', [$this, 'getActiveTheme']),
];
}
/**
......@@ -246,6 +268,16 @@ public function getLink($text, $url, array $attributes = []) {
return $build;
}
/**
* Gets the name of the active theme.
*
* @return string
* The name of the active theme.
*/
public function getActiveTheme() {
return $this->themeManager->getActiveTheme()->getName();
}
/**
* Determines at compile time whether the generated URL will be safe.
*
......
......@@ -72,4 +72,31 @@ public function providerTestEscaping() {
);
}
/**
* Tests the active_theme function.
*/
public function testActiveTheme() {
$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('getName')
->willReturn('test_theme');
$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() }}');
$this->assertEquals('test_theme', $result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment