How can I check to see if a theme is enabled on a drupal installation? I'm looking for something like module_exists() but for themes. I couldn't find anything on the documentation.
2 Answers
Use list_themes
Parameters
$refresh: Whether to reload the list of themes from the database. Defaults to FALSE.
Return value
An associative array of the currently available themes. The keys are the themes' machine names and the values are objects having the following properties:
- filename: The filepath and name of the .info file.
- name: The machine name of the theme.
- status: 1 for enabled, 0 for disabled themes.
- info: The contents of the .info file.
- stylesheets: A two dimensional array, using the first key for the media attribute (e.g. 'all'), the second for the name of the file (e.g. style.css). The value is a complete filepath (e.g. themes/bartik/style.css). Not set if no stylesheets are defined in the .info file.
- scripts: An associative array of JavaScripts, using the filename as key and the complete filepath as value. Not set if no scripts are defined in the .info file.
- prefix: The base theme engine prefix.
- engine: The machine name of the theme engine.
- base_theme: If this is a sub-theme, the machine name of the base theme defined in the .info file. Otherwise, the element is not set.
- base_themes: If this is a sub-theme, an associative array of the base-theme ancestors of this theme, starting with this theme's base theme, then the base theme's own base theme, etc. Each entry has an array key equal to the theme's machine name, and a value equal to the human-readable theme name; if a theme with matching machine name does not exist in the system, the value will instead be NULL (and since the system would not know whether that theme itself has a base theme, that will end the array of base themes). This is not set if the theme is not a sub-theme.
- sub_themes: An associative array of themes on the system that are either direct sub-themes (that is, they declare this theme to be their base theme), direct sub-themes of sub-themes, etc. The keys are the themes' machine names, and the values are the themes' human-readable names. This element is not set if there are no themes on the system that declare this theme as their base theme.
Use path_to_theme to get active theme..
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.
The data is actually held in exactly the same place, the system
table. I couldn't find an existing function to check (though there probably is one somewhere), but this should do in a pinch:
function theme_exists($theme_name) {
$themes = list_themes();
return isset($themes[$theme_name]) && $themes[$theme_name]->status == 1;
}
-
1I had just finished typing exactly the same thing -_- although I would probably not call the function
theme_exists
just to sidestep any possible issues with the theme system– ChapabuJan 30, 2013 at 13:44 -
1list_themes() already does static caching, so there's probably no need for that in this function. Jan 30, 2013 at 13:44
-
1
-
-
1I didn't mean that this function wasn't necessary, I think it's great. I was referring to your own statement that it could be improved with caching, which I don't think would be helpful in this instance. Jan 30, 2013 at 13:47