3 namespace Drupal\Core\Template\Loader;
5 use Drupal\Core\Theme\Registry;
8 * Loads templates based on information from the Drupal theme registry.
10 * Allows for template inheritance based on the currently active template.
12 class ThemeRegistryLoader extends \Twig_Loader_Filesystem {
15 * The theme registry used to determine which template to use.
17 * @var \Drupal\Core\Theme\Registry
19 protected $themeRegistry;
22 * Constructs a new ThemeRegistryLoader object.
24 * @param \Drupal\Core\Theme\Registry $theme_registry
27 public function __construct(Registry $theme_registry) {
28 $this->themeRegistry = $theme_registry;
32 * Finds the path to the requested template.
35 * The name of the template to load.
37 * Whether to throw an exception when an error occurs.
39 * @return string|false
40 * The path to the template, or false if the template is not found.
42 * @throws \Twig_Error_Loader
43 * Thrown if a template matching $name cannot be found.
45 protected function findTemplate($name, $throw = TRUE) {
46 // Allow for loading based on the Drupal theme registry.
47 $hook = str_replace('.html.twig', '', strtr($name, '-', '_'));
48 $theme_registry = $this->themeRegistry->getRuntime();
50 if ($theme_registry->has($hook)) {
51 $info = $theme_registry->get($hook);
52 if (isset($info['path'])) {
53 $path = $info['path'] . '/' . $name;
55 elseif (isset($info['template'])) {
56 $path = $info['template'] . '.html.twig';
58 if (isset($path) && is_file($path)) {
59 return $this->cache[$name] = $path;
64 throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name));