diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index bb29cd1..52bb06d 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -98,7 +98,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_path', array($this, 'activeThemePath')), ); } @@ -443,4 +444,15 @@ public function renderVar($arg) { return $this->renderer->render($arg); } + /** + * Gets the path of the active theme. + * + * @return string + * The path to the active theme. + */ + public function activeThemePath() { + return \Drupal::service('theme.manager')->getActiveTheme()->getPath(); + + } + } diff --git a/core/modules/system/src/Tests/Theme/EngineTwigTest.php b/core/modules/system/src/Tests/Theme/EngineTwigTest.php index ff84af7..64e125d 100644 --- a/core/modules/system/src/Tests/Theme/EngineTwigTest.php +++ b/core/modules/system/src/Tests/Theme/EngineTwigTest.php @@ -119,4 +119,13 @@ public function testTwigAttachLibrary() { $this->assertRaw('ckeditor.js'); } + /** + * Tests the active_theme_path() function. + */ + public function testActiveThemePath() { + $this->drupalGet('/twig-theme-test/active_theme_path'); + $theme_path = \Drupal::service('theme.manager')->getActiveTheme()->getPath(); + $this->assertRaw('
active theme path: ' . $theme_path . '
'); + } + } diff --git a/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php b/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php index 50d6c99..518d029 100644 --- a/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php +++ b/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php @@ -84,4 +84,11 @@ public function registryLoaderRender() { return array('#theme' => 'twig_registry_loader_test'); } + /** + * Menu callback for testing the active theme path function. + */ + public function activeThemePathRender() { + return array('#theme' => 'twig_theme_test_active_theme_path'); + } + } diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.active_theme_path.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.active_theme_path.html.twig new file mode 100644 index 0000000..a52e326 --- /dev/null +++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.active_theme_path.html.twig @@ -0,0 +1 @@ +
active theme path: {{ active_theme_path() }}
diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module index 021d039..8462ba9 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module @@ -54,6 +54,10 @@ function twig_theme_test_theme($existing, $type, $theme, $path) { 'variables' => array(), 'template' => 'twig_theme_test.attach_library', ); + $items['twig_theme_test_active_theme_path'] = array( + 'variables' => array(), + 'template' => 'twig_theme_test.active_theme_path', + ); return $items; } diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml index 4cdfeca..7bca891 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml @@ -55,3 +55,10 @@ twig_theme_test_registry_loader: no_cache: TRUE requirements: _access: 'TRUE' + +twig_theme_test_active_theme_path: + path: '/twig-theme-test/active_theme_path' + defaults: + _controller: '\Drupal\twig_theme_test\TwigThemeTestController::activeThemePathRender' + requirements: + _access: 'TRUE'