class ThemeHandler implements ThemeHandlerInterface (View source)

Default theme handler using the config system to store installation statuses.

Properties

protected array $list

A list of all currently available themes.

protected ConfigFactoryInterface $configFactory

The config factory to get the installed themes.

protected ThemeExtensionList $themeList

An extension discovery instance.

protected string $root

The app root.

Methods

__construct(string $root, ConfigFactoryInterface $config_factory, ThemeExtensionList $theme_list)

Constructs a new ThemeHandler.

string
getDefault()

Returns the default theme.

listInfo()

Returns a list of currently installed themes.

addTheme(Extension $theme)

Adds a theme extension to the internal listing.

refreshInfo()

Refreshes the theme info data of currently installed themes.

reset()

Resets the internal state of the theme handler.

rebuildThemeData()

Scans and collects theme extension data and their engines.

array
getBaseThemes(array $themes, string $theme)

Finds all the base themes for the specified theme.

string
getName(string $theme)

Gets the human readable name of a given theme.

array
getThemeDirectories()

Returns an array of directories for all installed themes.

bool
themeExists(string $theme)

Determines whether a given theme is installed.

getTheme(string $name)

Returns a theme extension object from the currently active theme list.

bool
hasUi(string $name)

Determines if a theme should be shown in the user interface.

Details

__construct(string $root, ConfigFactoryInterface $config_factory, ThemeExtensionList $theme_list)

Constructs a new ThemeHandler.

Parameters

string $root

The app root.

ConfigFactoryInterface $config_factory

The config factory to get the installed themes.

ThemeExtensionList $theme_list

An extension discovery instance.

string getDefault()

Returns the default theme.

Return Value

string

The default theme.

Extension[] listInfo()

Returns a list of currently installed themes.

Return Value

Extension[]

An associative array of the currently installed themes. The keys are the themes' machine names and the values are Extension objects having the following properties:

  • filename: The filepath and name of the .info.yml file.
  • name: The machine name of the theme.
  • status: 1 for installed, 0 for uninstalled themes.
  • info: The contents of the .info.yml 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.yml 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.yml 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.yml 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.

addTheme(Extension $theme)

Adds a theme extension to the internal listing.

Parameters

Extension $theme

The theme extension.

refreshInfo()

Refreshes the theme info data of currently installed themes.

Modules can alter theme info, so this is typically called after a module has been installed or uninstalled.

reset()

Resets the internal state of the theme handler.

Extension[] rebuildThemeData()

Scans and collects theme extension data and their engines.

Return Value

Extension[]

An associative array of theme extensions.

array getBaseThemes(array $themes, string $theme)

Finds all the base themes for the specified theme.

Themes can inherit templates and function implementations from earlier themes.

Parameters

array $themes

An array of available themes.

string $theme

The name of the theme whose base we are looking for.

Return Value

array

Returns an array of all of the theme's ancestors; the first element's value will be NULL if an error occurred.

string getName(string $theme)

Gets the human readable name of a given theme.

Parameters

string $theme

The machine name of the theme which title should be shown.

Return Value

string

Returns the human readable name of the theme.

Exceptions

UnknownExtensionException

array getThemeDirectories()

Returns an array of directories for all installed themes.

Useful for tasks such as finding a file that exists in all theme directories.

Return Value

array

bool themeExists(string $theme)

Determines whether a given theme is installed.

Parameters

string $theme

The name of the theme (without the .theme extension).

Return Value

bool

TRUE if the theme is installed.

Extension getTheme(string $name)

Returns a theme extension object from the currently active theme list.

Parameters

string $name

The name of the theme to return.

Return Value

Extension

An extension object.

Exceptions

UnknownExtensionException

bool hasUi(string $name)

Determines if a theme should be shown in the user interface.

To be shown in the UI the theme has to be installed. If the theme is hidden it will not be shown unless it is the default or admin theme.

Parameters

string $name

The name of the theme to check.

Return Value

bool

TRUE if the theme should be shown in the UI, FALSE if not.