Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Current Drupal Theme?

Within the context of a module, how can you determine what theme is loaded for the current user?

drupal_get_path
path_to_theme

Neither of these are any good because they seem to only work in the template.php of a theme.

like image 753
Kevin Avatar asked Jun 25 '26 07:06

Kevin


2 Answers

If users are allowed to select a theme for themselves, the theme they selected is saved in $user->theme, where $user is the user object. The global variable $custom_theme contains the name of the theme currently set, if a module has set a custom theme.

The following snippet saves in $current_theme the name of the theme currently active:

global $custom_theme, $theme, $user;

if (!empty($user->theme)) {
  $current_theme = $user->theme;
}
elseif (!empty($custom_theme)) {
  $current_theme = $custom_theme;
}
else {
  $current_theme = $theme ? $theme : variable_get('theme_default', 'garland');
}
like image 154
apaderno Avatar answered Jun 27 '26 20:06

apaderno


path_to_theme should work just fine, I tested it on two Drupal installs, and both worked. If the theme has not been initialized yet, path_to_theme will do that, which is what Drupal uses internally to set different global theme variables like $theme_path, which is the variable you are looking for.

like image 29
googletorp Avatar answered Jun 27 '26 22:06

googletorp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!