class Registry implements DestructableInterface (View source)

internal  Theme registry is expected to be used only internally since every hook_theme() implementation depends on the way this class is built. This class may get new features in minor releases so this class should be considered internal.
 

Defines the theme registry service.

Replace local $registry variables in methods with $this->registry.

Properties

protected ActiveTheme $theme

The theme object representing the active theme for this registry.

protected LockBackendInterface $lock

The lock backend that should be used.

protected array $registry

The complete theme registry.

protected CacheBackendInterface $cache

The cache backend to use for the complete theme registry data.

protected ModuleHandlerInterface $moduleHandler

The module handler to use to load modules.

protected ThemeRegistry[] $runtimeRegistry

An array of incomplete, runtime theme registries, keyed by theme name.

protected bool $initialized

Stores whether the registry was already initialized.

protected string|null $themeName

The name of the theme for which to construct the registry, if given.

protected string $root

The app root.

protected ThemeHandlerInterface $themeHandler

The theme handler.

protected ThemeInitializationInterface $themeInitialization

The theme initialization.

protected ThemeManagerInterface $themeManager

The theme manager.

protected CacheBackendInterface $runtimeCache

The runtime cache.

protected ModuleExtensionList $moduleList

The module list.

Methods

__construct(string $root, CacheBackendInterface $cache, LockBackendInterface $lock, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, ThemeInitializationInterface $theme_initialization, string $theme_name = NULL, CacheBackendInterface $runtime_cache = NULL, ModuleExtensionList $module_list = NULL)

Constructs a \Drupal\Core\Theme\Registry object.

setThemeManager(ThemeManagerInterface $theme_manager)

Sets the theme manager.

init(string $theme_name = NULL)

Initializes a theme with a certain name.

array
get()

Returns the complete theme registry from cache or rebuilds it.

getRuntime()

Returns the incomplete, runtime theme registry.

setCache()

Persists the theme registry in the cache backend.

string|false
getBaseHook(string $hook)

Returns the base hook for a given hook suggestion.

build()

Builds the theme registry cache.

processExtension(array $cache, string $name, string $type, string $theme, string $path)

Process a single implementation of hook_theme().

completeSuggestion(string $hook, array $cache)

Completes the definition of the requested suggestion hook.

mergePreprocessFunctions(string $destination_hook_name, string $source_hook_name, array $parent_hook, array $cache)

Merges the source hook's preprocess functions into the destination hook's.

postProcessExtension(array $cache, ActiveTheme $theme)

Completes the theme registry adding discovered functions and hooks.

reset()

Invalidates theme registry caches.

destruct()

Performs destruct operations.

array
getPrefixGroupedUserFunctions($prefixes = [])

Gets all user functions grouped by the word before the first underscore.

string
getPath(string $module) deprecated

Wraps drupal_get_path().

Details

__construct(string $root, CacheBackendInterface $cache, LockBackendInterface $lock, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, ThemeInitializationInterface $theme_initialization, string $theme_name = NULL, CacheBackendInterface $runtime_cache = NULL, ModuleExtensionList $module_list = NULL)

Constructs a \Drupal\Core\Theme\Registry object.

Parameters

string $root

The app root.

CacheBackendInterface $cache

The cache backend interface to use for the complete theme registry data.

LockBackendInterface $lock

The lock backend.

ModuleHandlerInterface $module_handler

The module handler to use to load modules.

ThemeHandlerInterface $theme_handler

The theme handler.

ThemeInitializationInterface $theme_initialization

The theme initialization.

string $theme_name

(optional) The name of the theme for which to construct the registry.

CacheBackendInterface $runtime_cache

The cache backend interface to use for the runtime theme registry data.

ModuleExtensionList $module_list

The module list.

setThemeManager(ThemeManagerInterface $theme_manager)

Sets the theme manager.

Parameters

ThemeManagerInterface $theme_manager

The theme manager.

protected init(string $theme_name = NULL)

Initializes a theme with a certain name.

This function does to much magic, so it should be replaced by another services which holds the current active theme information.

Parameters

string $theme_name

(optional) The name of the theme for which to construct the registry.

array get()

Returns the complete theme registry from cache or rebuilds it.

Return Value

array

The complete theme registry data array.

See also

Registry::$registry

ThemeRegistry getRuntime()

Returns the incomplete, runtime theme registry.

Return Value

ThemeRegistry

A shared instance of the ThemeRegistry class, provides an ArrayObject that allows it to be accessed with array syntax and isset(), and is more lightweight than the full registry.

protected setCache()

Persists the theme registry in the cache backend.

string|false getBaseHook(string $hook)

Returns the base hook for a given hook suggestion.

Parameters

string $hook

The name of a theme hook whose base hook to find.

Return Value

string|false

The name of the base hook or FALSE.

protected ThemeRegistry build()

Builds the theme registry cache.

Theme hook definitions are collected in the following order:

  • Modules
  • Base theme engines
  • Base themes
  • Theme engine
  • Theme

All theme hook definitions are essentially just collated and merged in the above order. However, various extension-specific default values and customizations are required; e.g., to record the effective file path for theme template. Therefore, this method first collects all extensions per type, and then dispatches the processing for each extension to processExtension().

After completing the collection, modules are allowed to alter it. Lastly, any derived and incomplete theme hook definitions that are hook suggestions for base hooks (e.g., 'block__node' for the base hook 'block') need to be determined based on the full registry and classified as 'base hook'.

See the @link themeable Default theme implementations topic @endlink for details.

Return Value

ThemeRegistry

The build theme registry.

See also

hook_theme_registry_alter()

protected processExtension(array $cache, string $name, string $type, string $theme, string $path)

Process a single implementation of hook_theme().

Parameters

array $cache

The theme registry that will eventually be cached; It is an associative array keyed by theme hooks, whose values are associative arrays describing the hook:

  • 'type': The passed-in $type.
  • 'theme path': The passed-in $path.
  • 'function': The name of the function generating output for this theme hook. Either defined explicitly in hooktheme() or, if neither 'function' nor 'template' is defined, then the default theme function name is used. The default theme function name is the theme hook prefixed by either 'theme' for modules or '$name_' for everything else. If 'function' is defined, 'template' is not used.
  • 'template': The filename of the template generating output for this theme hook. The template is in the directory defined by the 'path' key of hook_theme() or defaults to "$path/templates".
  • 'variables': The variables for this theme hook as defined in hook_theme(). If there is more than one implementation and 'variables' is not specified in a later one, then the previous definition is kept.
  • 'render element': The renderable element for this theme hook as defined in hook_theme(). If there is more than one implementation and 'render element' is not specified in a later one, then the previous definition is kept.
  • See the @link themeable Theme system overview topic @endlink for detailed documentation.
string $name

The name of the module, theme engine, base theme engine, theme or base theme implementing hook_theme().

string $type

One of 'module', 'theme_engine', 'base_theme_engine', 'theme', or 'base_theme'. Unlike regular hooks that can only be implemented by modules, each of these can implement hook_theme(). This function is called in aforementioned order and new entries override older ones. For example, if a theme hook is both defined by a module and a theme, then the definition in the theme will be used.

string $theme

The actual name of theme, module, etc. that is being processed.

string $path

The directory where $name is. For example, modules/system or themes/bartik.

Exceptions

BadFunctionCallException

See also

ThemeManagerInterface::render
hook_theme()
ThemeHandler::listInfo
twig_render_template()

protected completeSuggestion(string $hook, array $cache)

Completes the definition of the requested suggestion hook.

Parameters

string $hook

The name of the suggestion hook to complete.

array $cache

The theme registry, as documented in \Drupal\Core\Theme\Registry::processExtension().

protected mergePreprocessFunctions(string $destination_hook_name, string $source_hook_name, array $parent_hook, array $cache)

Merges the source hook's preprocess functions into the destination hook's.

Parameters

string $destination_hook_name

The name of the hook to merge preprocess functions to.

string $source_hook_name

The name of the hook to merge preprocess functions from.

array $parent_hook

The parent hook if it exists. Either an incomplete hook from suggestions or a base hook.

array $cache

The theme registry, as documented in \Drupal\Core\Theme\Registry::processExtension().

protected postProcessExtension(array $cache, ActiveTheme $theme)

Completes the theme registry adding discovered functions and hooks.

Parameters

array $cache

The theme registry as documented in \Drupal\Core\Theme\Registry::processExtension().

ActiveTheme $theme

Current active theme.

See also

\Drupal\Core\Theme\::processExtension()

reset()

Invalidates theme registry caches.

To be called when the list of enabled extensions is changed.

destruct()

Performs destruct operations.

array getPrefixGroupedUserFunctions($prefixes = [])

Gets all user functions grouped by the word before the first underscore.

Parameters

$prefixes

An array of function prefixes by which the list can be limited.

Return Value

array

Functions grouped by the first prefix.

protected string getPath(string $module) deprecated

deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\Core\Extension\ExtensionList::getPath() instead.

Wraps drupal_get_path().

Parameters

string $module

The name of the item for which the path is requested.

Return Value

string

See also

https://www.drupal.org/node/2940438