class ThemeRegistryLoader
Loads templates based on information from the Drupal theme registry.
Allows for template inheritance based on the currently active template.
Hierarchy
- class \Twig\Loader\FilesystemLoader implements \Twig\Loader\LoaderInterface
- class \Drupal\Core\Template\Loader\ThemeRegistryLoader extends \Twig\Loader\FilesystemLoader
Expanded class hierarchy of ThemeRegistryLoader
File
-
core/
lib/ Drupal/ Core/ Template/ Loader/ ThemeRegistryLoader.php, line 14
Namespace
Drupal\Core\Template\LoaderView source
class ThemeRegistryLoader extends FilesystemLoader {
/**
* The theme registry used to determine which template to use.
*
* @var \Drupal\Core\Theme\Registry
*/
protected $themeRegistry;
/**
* Constructs a new ThemeRegistryLoader object.
*
* @param \Drupal\Core\Theme\Registry $theme_registry
* The theme registry.
*/
public function __construct(Registry $theme_registry) {
$this->themeRegistry = $theme_registry;
}
/**
* Finds the path to the requested template.
*
* @param string $name
* The name of the template to load.
* @param bool $throw
* Whether to throw an exception when an error occurs.
*
* @return string|null
* The path to the template, or NULL if the template is not found.
*
* @throws \Twig\Error\LoaderError
* Thrown if a template matching $name cannot be found.
*/
protected function findTemplate(string $name, bool $throw = TRUE) {
// Allow for loading based on the Drupal theme registry.
$hook = str_replace('.html.twig', '', strtr($name, '-', '_'));
$theme_registry = $this->themeRegistry
->getRuntime();
if ($theme_registry->has($hook)) {
$info = $theme_registry->get($hook);
if (isset($info['path'])) {
$path = $info['path'] . '/' . $name;
}
elseif (isset($info['template'])) {
$path = $info['template'] . '.html.twig';
}
if (isset($path) && is_file($path)) {
return $this->cache[$name] = $path;
}
}
if ($throw) {
throw new LoaderError(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name));
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function getCacheKey(string $name) : string {
// The parent implementation does unnecessary work that triggers
// deprecations in PHP 8.1.
return $this->findTemplate($name) ?: '';
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
FilesystemLoader::$cache | protected | property | |||
FilesystemLoader::$errorCache | protected | property | |||
FilesystemLoader::$paths | protected | property | |||
FilesystemLoader::$rootPath | private | property | |||
FilesystemLoader::addPath | public | function | 1 | ||
FilesystemLoader::exists | public | function | Overrides LoaderInterface::exists | ||
FilesystemLoader::getNamespaces | public | function | Returns the path namespaces. | ||
FilesystemLoader::getPaths | public | function | Returns the paths to the templates. | ||
FilesystemLoader::getSourceContext | public | function | Returns the source context for a given template logical name. | Overrides LoaderInterface::getSourceContext | 1 |
FilesystemLoader::isAbsolutePath | private | function | |||
FilesystemLoader::isFresh | public | function | Overrides LoaderInterface::isFresh | ||
FilesystemLoader::MAIN_NAMESPACE | public | constant | Identifier of the main namespace. | 1 | |
FilesystemLoader::normalizeName | private | function | |||
FilesystemLoader::parseName | private | function | |||
FilesystemLoader::prependPath | public | function | |||
FilesystemLoader::setPaths | public | function | |||
FilesystemLoader::validateName | private | function | |||
ThemeRegistryLoader::$themeRegistry | protected | property | The theme registry used to determine which template to use. | ||
ThemeRegistryLoader::findTemplate | protected | function | Finds the path to the requested template. | Overrides FilesystemLoader::findTemplate | |
ThemeRegistryLoader::getCacheKey | public | function | Gets the cache key to use for the cache for a given template name. | Overrides FilesystemLoader::getCacheKey | |
ThemeRegistryLoader::__construct | public | function | Constructs a new ThemeRegistryLoader object. | Overrides FilesystemLoader::__construct |