core/core.services.yml | 5 ++ .../Core/Template/Loader/ThemeRegistryLoader.php | 68 ++++++++++++++++++++++ .../src/Tests/Theme/TwigRegistryLoaderTest.php | 8 +++ 3 files changed, 81 insertions(+) diff --git a/core/core.services.yml b/core/core.services.yml index 3fdc792..e89175e 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1541,6 +1541,11 @@ services: arguments: ['@app.root', '@module_handler', '@theme_handler'] tags: - { name: twig.loader, priority: 100 } + twig.loader.theme_registry: + class: Drupal\Core\Template\Loader\ThemeRegistryLoader + arguments: ['@theme.registry'] + tags: + - { name: twig.loader, priority: 0 } twig.loader.string: class: Drupal\Core\Template\Loader\StringLoader tags: diff --git a/core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php b/core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php new file mode 100644 index 0000000..67a82dd --- /dev/null +++ b/core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php @@ -0,0 +1,68 @@ +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 + * The path to the template. + * + * @throws \Twig_Error_Loader + * Thrown if a template matching $name cannot be found. + */ + protected function findTemplate($name, $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 \Twig_Error_Loader(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name)); + } + } + +} diff --git a/core/modules/system/src/Tests/Theme/TwigRegistryLoaderTest.php b/core/modules/system/src/Tests/Theme/TwigRegistryLoaderTest.php index 05b7b30..d90a484 100644 --- a/core/modules/system/src/Tests/Theme/TwigRegistryLoaderTest.php +++ b/core/modules/system/src/Tests/Theme/TwigRegistryLoaderTest.php @@ -29,6 +29,7 @@ class TwigRegistryLoaderTest extends WebTestBase { protected function setUp() { parent::setUp(); \Drupal::service('theme_handler')->install(array('test_theme_twig_registry_loader', 'test_theme_twig_registry_loader_theme', 'test_theme_twig_registry_loader_subtheme')); + $this->twig = \Drupal::service('twig'); } /** @@ -38,6 +39,13 @@ public function assertTwigTemplate($value, $message = '', $group = 'Other') { $this->assertTrue($value instanceof \Twig_Template, $message, $group); } + /** + * Tests template discovery using the Drupal theme registry. + */ + public function testTemplateDiscovery() { + $this->assertTwigTemplate($this->twig->resolveTemplate('block.html.twig'), 'Found block.html.twig in block module.'); + } + /** * Tests template extension and includes using the Drupal theme registry. */