diff --git a/core/core.services.yml b/core/core.services.yml index 3dc14f4..551d881 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -949,7 +949,7 @@ services: alias: twig.loader.filesystem twig.loader.filesystem: class: Drupal\Core\Template\TwigThemeRegistryLoader - arguments: ['%app.root%'] + arguments: ['%app.root%', '@theme.registry'] element_info: alias: plugin.manager.element_info file.mime_type.guesser: diff --git a/core/lib/Drupal/Core/Template/TwigThemeRegistryLoader.php b/core/lib/Drupal/Core/Template/TwigThemeRegistryLoader.php index 44f8121..ec6e17e 100644 --- a/core/lib/Drupal/Core/Template/TwigThemeRegistryLoader.php +++ b/core/lib/Drupal/Core/Template/TwigThemeRegistryLoader.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Template; +use Drupal\Core\Theme\Registry; + /** * Registers paths based on information from the Drupal theme registry. * @@ -15,6 +17,28 @@ class TwigThemeRegistryLoader extends \Twig_Loader_Filesystem { /** + * The theme registry used to determine which template to use. + * + * @var \Drupal\Core\Theme\Registry + */ + protected $themeRegistry; + + /** + * Constructs a new TwigThemeRegistryLoader object. + * + * @param string|array $paths + * A path or an array of paths where to look for templates + * @param \Drupal\Core\Theme\Registry $theme_registry + * The theme registry. + */ + public function __construct($paths = array(), Registry $theme_registry) { + if ($paths) { + $this->setPaths($paths); + } + $this->themeRegistry = $theme_registry; + } + + /** * Finds the path to the requested template. * * @param string $name @@ -27,6 +51,50 @@ class TwigThemeRegistryLoader extends \Twig_Loader_Filesystem { * Thrown if a template matching $name cannot be found. */ protected function findTemplate($name) { + // Go through the normal findTemplate() checks based on path and namespaced + // path. + if ($default = $this->findTemplateDefault($name)) { + return $default; + } + + // 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; + } + } + + throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]))); + } + + /** + * Finds the path to the requested template using standard methods. + * + * This is copied from Twig_Loader_Filesystem::findTemplate() and allows + * loading based on the path or namespaced path. The final throw has been + * removed and is in findTemplate() instead to allow additional loading based + * on the Drupal theme registry. + * + * @param string $name + * The name of the template to load. + * + * @return string + * The path to the template if one is found. + * + * @throws Twig_Error_Loader + * Thrown if namespace-related errors occur. + */ + protected function findTemplateDefault($name) { $name = (string) $name; // Normalize name. @@ -58,25 +126,6 @@ protected function findTemplate($name) { 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]))); } }