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?
Use the path_to_theme
function.
this should work (doc):
global $theme;
$path = drupal_get_path('theme', $theme);
// there's also a $theme_path global
global $theme_path;
path_to_theme()
, than to use $theme_path
. –
Saccharoid $theme_path
is not set the theme gets initialized and then the new $theme_path
is returned. See path_to_theme. –
Intelligence 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.
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();
In Drupal 7, for getting current theme's path, we can use: path_to_theme() function.
In Drupal 8
global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$image_url = $base_url.'/'. $theme->getPath() .'/images/image.jpg';
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.
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');
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 }}">
© 2022 - 2024 — McMap. All rights reserved.