How do I get the path of the current drupal theme?
Asked Answered
D

9

29

The Drupal API has drupal_get_path($type, $name) which will give the path of any particular theme or module. What if I want the path of the current theme?

Derision answered 19/11, 2008 at 23:14 Comment(1)
Be careful using the current theme path. If somebody creates a subtheme from your theme, the current theme path will be the subtheme's path, not yours! You may break your own theme or the subtheme. - D8 at leastSimpson
S
27

Use the path_to_theme function.

Sportswoman answered 20/11, 2008 at 18:38 Comment(1)
note that when called from inside "theme_*" function path_to_there will return path to current module instead of path to currently active theme which can lead to errors, see more details in this bug report:drupal.org/node/194098Lxx
D
20

this should work (doc):

global $theme;
$path = drupal_get_path('theme', $theme);

// there's also a $theme_path global

global $theme_path;
Duthie answered 19/11, 2008 at 23:25 Comment(4)
It's better to use path_to_theme(), than to use $theme_path.Saccharoid
Why is it better to use path_to_theme(), instead of $theme_path?Aguiar
@timoxley, the only difference is that if the $theme_path is not set the theme gets initialized and then the new $theme_path is returned. See path_to_theme.Intelligence
I noticed path_to_theme() and $theme_path used inside a field template is giving me the path to module field/module. The only that displayed correctly was $theme.Coplin
C
10

In D6 path_to_theme() may not behave in a way you expect depending on how you are using it. If you are using it outside any theme preprocess functions, then it will probably give you what you want, but if it is being called within the context of a module's theming/preprocess hook function... it will be pointing to the module path that declared the theme.

Ex. If i have a theme "my_theme" and my module "my_module" which is overriding the forum themes using the preprocess hooks, calling path_to_theme() within my module: e.g. my_module_preprocess_forums()... will return "forums", and not "my_theme" as one might expect.

Very fruity if you ask me.

Charissecharita answered 3/3, 2010 at 20:40 Comment(0)
V
10

In Drupal 8, if you need to get the active theme path when you have the admin theme active you can fetch the default theme path:

$themeHandler = \Drupal::service('theme_handler');
$themePath = $themeHandler->getTheme($themeHandler->getDefault())->getPath();
Vampire answered 11/11, 2019 at 12:17 Comment(1)
Best answer for Drupal 8/9 in my opinionMantis
D
5

In Drupal 7, for getting current theme's path, we can use: path_to_theme() function.

Determined answered 7/2, 2012 at 12:44 Comment(0)
M
5

In Drupal 8

global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$image_url = $base_url.'/'. $theme->getPath() .'/images/image.jpg';
Mosora answered 29/1, 2018 at 9:30 Comment(1)
This is not the best way - see other answer.Roofdeck
S
1

In Drupal 5, you can simply use: path_to_theme()

This will give you a complete path from the root of Drupal to the specific theme directory. Be aware, it does not include a trailing slash.

In Drupal 6, this behaves just a bit differently. If you call it from within your pages, it will call whatever is currently doing the theming... whether that is your theme, a module, etc. Here's the key quote from the API docs:

It can point to the active theme or the module handling a themed implementation. For example, when invoked within the scope of a theming call it will depend on where the theming function is handled. If implemented from a module, it will point to the module. If implemented from the active theme, it will point to the active theme. When called outside the scope of a theming call, it will always point to the active theme.

Source: http://api.drupal.org/api/function/path_to_theme

Sluiter answered 12/12, 2008 at 17:46 Comment(0)
S
1

If you already know the theme name

$themePath = \Drupal::service('extension.list.theme')->getPath('my_theme_name');

or

 $themePath = \Drupal::service('extension.path.resolver')->getPath('theme', 'my_theme_name');
Southbound answered 19/1, 2023 at 8:53 Comment(0)
B
0

For D8, the theme folder is available in preprocess functions:

function hook_preprocess_page(&$variables) {
  $variables['some_logo_file'] = "/{$variables['theme']['path']}/images/logo.png";
}

page.html.twig:

<img src="{{ logo_src }}">
Benne answered 20/12, 2017 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.