Unverified Commit 260476c4 authored by alexpott's avatar alexpott

Issue #2822470 by Kazanir, Yogesh Pawar, markcarver, joelpittet, dawehner,...

Issue #2822470 by Kazanir, Yogesh Pawar, markcarver, joelpittet, dawehner, xjm, larowlan: Unified cache key & template class handling for Twig templates
parent 018bd7c5
......@@ -1618,7 +1618,10 @@ services:
- { name: service_collector, tag: twig.loader, call: addLoader, required: TRUE }
twig.loader.filesystem:
class: Drupal\Core\Template\Loader\FilesystemLoader
arguments: ['@app.root', '@module_handler', '@theme_handler']
# We use '.' instead of '@app.root' as the path for non-namespaced template
# files so that they match the relative paths of templates loaded via the
# theme registry or via Twig namespaces.
arguments: ['.', '@module_handler', '@theme_handler']
tags:
- { name: twig.loader, priority: 100 }
twig.loader.theme_registry:
......
......@@ -111,6 +111,35 @@ public function testTemplateNotFoundException() {
}
}
/**
* Ensures that templates resolve to the same class name and cache file.
*/
public function testTemplateClassname() {
/** @var \Drupal\Core\Template\TwigEnvironment $environment */
$environment = \Drupal::service('twig');
// Test using an include template path.
$name_include = 'container.html.twig';
$class_include = $environment->getTemplateClass($name_include);
$key_include = $environment->getCache()->generateKey($name_include, $class_include);
// Test using a namespaced template path.
$name_namespaced = '@system/container.html.twig';
$class_namespaced = $environment->getTemplateClass($name_namespaced);
$key_namespaced = $environment->getCache()->generateKey($name_namespaced, $class_namespaced);
// Test using a direct filesystem template path.
$name_direct = 'core/modules/system/templates/container.html.twig';
$class_direct = $environment->getTemplateClass($name_direct);
$key_direct = $environment->getCache()->generateKey($name_direct, $class_direct);
// All three should be equal for both cases.
$this->assertEqual($class_include, $class_namespaced);
$this->assertEqual($class_namespaced, $class_direct);
$this->assertEqual($key_include, $key_namespaced);
$this->assertEqual($key_namespaced, $key_direct);
}
/**
* Ensures that cacheFilename() varies by extensions + deployment identifier.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment