Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Steps to reproduce
- Install 8.2.x
- Install the
amptheme
theme - Go to the home page
The website encountered an unexpected error. Please try again later.
Twig_Error_Loader: Template "themes/contrib/amptheme/amptheme/templates/amp-skip-link.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "themes/contrib/amptheme/amptheme/templates/amp-skip-link.html.twig" in the Drupal theme registry.) in "themes/contrib/amptheme/amptheme/templates/layout/html.html.twig" at line 42. in Twig_Loader_Chain->getCacheKey() (line 115 of /Users/dawehner/www/d8c/vendor/twig/twig/lib/Twig/Loader/Chain.php).
Drupal\Core\Template\TwigEnvironment->getTemplateClass('themes/contrib/amptheme/amptheme/templates/amp-skip-link.html.twig', NULL) (Line: 388)
Twig_Environment->loadTemplate('themes/contrib/amptheme/amptheme/templates/amp-skip-link.html.twig') (Line: 64)
twig_render_template('themes/contrib/amptheme/amptheme/templates/amp-skip-link.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('amp_skip_link', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 468)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 89)
__TwigTemplate_67a840ecfa59aae9188d35cda14eeee8e22c3044f602bfa75942b7ebcd707fda->doDisplay(Array, Array) (Line: 382)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 350)
Twig_Template->display(Array) (Line: 361)
Twig_Template->render(Array) (Line: 64)
twig_render_template('themes/contrib/amptheme/amptheme/templates/layout/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 652)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Proposed resolution
Fix it
Comment | File | Size | Author |
---|---|---|---|
#38 | theme_registry_doesn_t-2817103-38.patch | 3.99 KB | B-Prod |
#28 | theme_registry_doesn_t-2817103-28.patch | 3.91 KB | cameron prince |
#24 | theme_registry_doesn_t-2817103-23.patch | 3.89 KB | joelpittet |
#23 | theme_registry_doesn_t-2817103-23.patch | 0 bytes | joelpittet |
#19 | interdiff.txt | 3.93 KB | lauriii |
Comments
Comment #2
dawehnerThis never worked ... it was just hidden by a message before: #2706683: Don't convert twig exceptions to drupal messages and no longer is.
Comment #3
BerdirTrying to update the title to reflect what we figured out
Comment #4
dawehnerThank you berdir!
Comment #5
joelpittetThis is reproducible without the amp module, removed that from the steps to reproduce.
Comment #6
dawehner@joelpittet
So yeah the underlying reason is that the file based discovery is done in the
twig_theme()
method, whilehook_theme()
from themes run later.Comment #7
joelpittetThe
drupal_find_theme_templates()
uses the cache built up from all the modules implementation ofhook_theme()
buttwig_theme()
gets called before the theme's implementations ofhook_theme()
There is some unrelated code removal in here for
$template_candidates
, but I'm curious that that shouldn't break anything because that should never have a filename in the 'theme path' key from what I saw stepping through.So the patch just moves the theme engines'
hook_theme()
calls lastComment #8
lauriiiIt would be nice to have test coverage to prove this use case is working so that we don't break it in the future
Comment #9
joelpittetHere's a test that demos the problem.
Comment #11
lauriiiThe fix looks correct to me based on the assumptions that we make in core. I'm not sure if there are any themes out there that are expecting that theme hook for the theme engines has been called before themes. This could be possible but mostly very unlikely.
I looked back on the issue where this bit of code was originally introduced #137211: Move discovery of theme information to .info files (and improve theme inheritance) and couldn't find any notes about the order. Because of that, I am okay with this change.
Feel free to rename the theme hook name to tell what we are testing here. It is hard to remember in future what it's testing without looking at the test class.
Comment #12
joelpittetChanged the theme hook name.
Comment #13
lauriiiThanks @joelpittet. Still RTBC!
Comment #15
joelpittetI guess now we know
render element
is default for hooks. Addingvariables
key back in passes tests.Back to RTBC there isn't anything different in the patch besides the lack of that key.
Comment #16
xjmPer @joelpittet, this hunk should actually be removed from this patch.
@lauriii, @joelpittet, @Cottser, @alexpott, and I discussed the issue at length and decided to defer triage on this. Per @lauriii, this is a contributed project blocker for work on theme components. It might be more properly a major task.
@Cottser had some concerns about the risk of the change, so @alexpott, @Cottser, and I agreed that at a minimum this change would need to be minor-only. We might also consider another solution to reduce the risk (e.g. adding a new hook). Assigning to @Cottser for further committer review.
Comment #17
joelpittetRemoved the theme.inc change and moving that to a separate issue because it doesn't relate to this fix, just a by product of this fix.
#2821420: drupal_find_theme_templates() searches candidates that are never have templates and are duplicates
Comment #18
lauriiiI'm working on addressing concerns that @Cottser might have by adding a new post_theme hook for the theme engines for this purpose.
Comment #19
lauriiiLet's see if this works
Comment #20
joelpittet@lauriii, that looks like it makes the scope of this change bigger, could you explain your changes a bit more please.
Before it was just changing the order from:
to
What are the other changes and why are they needed?
Comment #21
dawehnerWe should document why we are doing that/why this is needed
Comment #23
joelpittetReposting my patch from #17 because of what I mentioned in ##2 and #19 introduced an API change with changing
twig_theme
totwig_theme_post_build
@lauriii please explain the changes further before reposting #19
Comment #24
joelpittetComment #26
lauriiiTwo reasons why I think we should take the approach from #19:
The point you brought up in #23 is quite interesting and I haven't thought about it before. The interesting part is that twig_theme is a hook, and therefore it shouldn't be called directly. However, it looks like we don't have anything about this in the Drupal 8 BC policy.
Comment #28
cameron prince CreditAttribution: cameron prince at Mediacurrent commentedRe-rolled against 8.5.x.
Comment #36
ciss CreditAttribution: ciss at yousign GmbH commentedConfirming that the description in #7 is spot-on. I'd like to add that this also breaks the use of theme suggestions for hooks that are defined in a theme, causing suggestions to always use the base hook template (even though preprocessing for suggestions will run, if implemented).
Comment #38
B-Prod CreditAttribution: B-Prod at Aïon Solutions commentedRe-roll of the patch for D9.
Comment #41
xjm