Theme
Helper service for working with themes.
- Full name:
\Drupal\helper\Theme
Properties
activeThemeStack
A stack of previously overridden themes.
protected \Drupal\Core\Theme\ActiveTheme[] $activeThemeStack
themeHandler
protected \Drupal\Core\Extension\ThemeHandlerInterface $themeHandler
themeManager
protected \Drupal\Core\Theme\ThemeManagerInterface $themeManager
themeInstaller
protected \Drupal\Core\Extension\ThemeInstallerInterface $themeInstaller
configFactory
protected \Drupal\Core\Config\ConfigFactoryInterface $configFactory
themeInitialization
protected \Drupal\Core\Theme\ThemeInitializationInterface $themeInitialization
themeExtensionList
protected \Drupal\Core\Extension\ThemeExtensionList $themeExtensionList
Methods
__construct
Constructs a Theme helper.
public __construct(\Drupal\Core\Extension\ThemeHandlerInterface $themeHandler, \Drupal\Core\Theme\ThemeManagerInterface $themeManager, \Drupal\Core\Extension\ThemeInstallerInterface $themeInstaller, \Drupal\Core\Config\ConfigFactoryInterface $configFactory, \Drupal\Core\Theme\ThemeInitializationInterface $themeInitialization, \Drupal\Core\Extension\ThemeExtensionList $themeExtensionList): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$themeHandler |
\Drupal\Core\Extension\ThemeHandlerInterface | |
| Theme handler. | ||
$themeManager |
\Drupal\Core\Theme\ThemeManagerInterface | |
| Theme manager. | ||
$themeInstaller |
\Drupal\Core\Extension\ThemeInstallerInterface | |
| Theme installer. | ||
$configFactory |
\Drupal\Core\Config\ConfigFactoryInterface | |
| Configuration factory. | ||
$themeInitialization |
\Drupal\Core\Theme\ThemeInitializationInterface | |
| Theme initialization. | ||
$themeExtensionList |
\Drupal\Core\Extension\ThemeExtensionList | |
| Theme extension list. |
isActive
Checks if a theme is the active theme or one of the active base themes.
public isActive(string $theme_name, bool $check_base_themes = false): bool
Parameters:
| Parameter | Type | Description |
|---|---|---|
$theme_name |
string | |
| Theme name. | ||
$check_base_themes |
bool | |
| TRUE to check if the theme is in the base themes of the active theme. |
Return Value:
TRUE if the theme is active, or FALSE otherwise.
isDefault
Checks if a theme is the default theme or one of the default base themes.
public isDefault(string $theme_name, bool $check_base_themes = false): bool
Parameters:
| Parameter | Type | Description |
|---|---|---|
$theme_name |
string | |
| Theme name. | ||
$check_base_themes |
bool | |
| TRUE to check if the theme is in the base themes of the default theme. |
Return Value:
TRUE if the theme is default, or FALSE otherwise.
isBaseTheme
Checks if a theme is a base theme of a sub-theme.
public isBaseTheme(string $base_theme, string|null $sub_theme_name = null): bool
Parameters:
| Parameter | Type | Description |
|---|---|---|
$base_theme |
string | |
| The theme that is potentially a base theme. | ||
$sub_theme_name |
string|null | |
| The sub theme name to check for the base theme. |
Return Value:
TRUE if the theme is a base theme or FALSE otherwise.
setDefault
Sets the default theme.
public setDefault(string $theme_name): bool
Parameters:
| Parameter | Type | Description |
|---|---|---|
$theme_name |
string | |
| Theme name. |
Return Value:
TRUE if the default theme was updated, or FALSE if the requested theme was already the default.
switch
Switches to the specified theme.
public switch(string $theme_name): bool
Parameters:
| Parameter | Type | Description |
|---|---|---|
$theme_name |
string | |
| Theme name. |
Throws:
If the theme does not exist or is not installed.
- InvalidArgumentException
switchBack
Switches back to the previous theme.
public switchBack(): void
Throws:
If there are no more themes to revert to.
- RuntimeException