862092b6062a0d085b99ea19d4ecc6fd71139acf
[yaffs-website] / web / core / lib / Drupal / Core / Template / Loader / ThemeRegistryLoader.php
1 <?php
2
3 namespace Drupal\Core\Template\Loader;
4
5 use Drupal\Core\Theme\Registry;
6
7 /**
8  * Loads templates based on information from the Drupal theme registry.
9  *
10  * Allows for template inheritance based on the currently active template.
11  */
12 class ThemeRegistryLoader extends \Twig_Loader_Filesystem {
13
14   /**
15    * The theme registry used to determine which template to use.
16    *
17    * @var \Drupal\Core\Theme\Registry
18    */
19   protected $themeRegistry;
20
21   /**
22    * Constructs a new ThemeRegistryLoader object.
23    *
24    * @param \Drupal\Core\Theme\Registry $theme_registry
25    *   The theme registry.
26    */
27   public function __construct(Registry $theme_registry) {
28     $this->themeRegistry = $theme_registry;
29   }
30
31   /**
32    * Finds the path to the requested template.
33    *
34    * @param string $name
35    *   The name of the template to load.
36    * @param bool $throw
37    *   Whether to throw an exception when an error occurs.
38    *
39    * @return string|false
40    *   The path to the template, or false if the template is not found.
41    *
42    * @throws \Twig_Error_Loader
43    *   Thrown if a template matching $name cannot be found.
44    */
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();
49
50     if ($theme_registry->has($hook)) {
51       $info = $theme_registry->get($hook);
52       if (isset($info['path'])) {
53         $path = $info['path'] . '/' . $name;
54       }
55       elseif (isset($info['template'])) {
56         $path = $info['template'] . '.html.twig';
57       }
58       if (isset($path) && is_file($path)) {
59         return $this->cache[$name] = $path;
60       }
61     }
62
63     if ($throw) {
64       throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name));
65     }
66
67     return FALSE;
68   }
69
70 }