diff --git a/core/core.services.yml b/core/core.services.yml index c26834c..3dc14f4 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -948,7 +948,7 @@ services: twig.loader: alias: twig.loader.filesystem twig.loader.filesystem: - class: Twig_Loader_Filesystem + class: Drupal\Core\Template\TwigThemeRegistryLoader arguments: ['%app.root%'] element_info: alias: plugin.manager.element_info diff --git a/core/lib/Drupal/Core/Template/TwigThemeRegistryLoader.php b/core/lib/Drupal/Core/Template/TwigThemeRegistryLoader.php new file mode 100644 index 0000000..acfda12 --- /dev/null +++ b/core/lib/Drupal/Core/Template/TwigThemeRegistryLoader.php @@ -0,0 +1,75 @@ +cache[$name])) { + return $this->cache[$name]; + } + + $this->validateName($name); + + $namespace = self::MAIN_NAMESPACE; + $shortname = $name; + if (isset($name[0]) && '@' == $name[0]) { + if (false === $pos = strpos($name, '/')) { + throw new \Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name)); + } + + $namespace = substr($name, 1, $pos - 1); + $shortname = substr($name, $pos + 1); + } + + if (!isset($this->paths[$namespace])) { + throw new \Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace)); + } + + foreach ($this->paths[$namespace] as $path) { + if (is_file($path . '/' . $shortname)) { + return $this->cache[$name] = $path . '/' . $shortname; + } + } + + // Allow for loading based on the theme registry. + $hook = str_replace('.html.twig', '', strtr($name, '-', '_')); + $theme_registry = \Drupal::service('theme.registry')->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; + } + } + + throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]))); + } + +}