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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner created an issue. See original summary.

dawehner’s picture

FileSize
1.15 KB

This never worked ... it was just hidden by a message before: #2706683: Don't convert twig exceptions to drupal messages and no longer is.

Berdir’s picture

Title: Theme registry doesn't find theme defined hook_theme() anymore » Theme registry doesn't find templates in subfolders for self-defined theme hooks

Trying to update the title to reflect what we figured out

dawehner’s picture

Thank you berdir!

joelpittet’s picture

Issue summary: View changes

This is reproducible without the amp module, removed that from the steps to reproduce.

dawehner’s picture

@joelpittet
So yeah the underlying reason is that the file based discovery is done in the twig_theme() method, while hook_theme() from themes run later.

joelpittet’s picture

Status: Active » Needs review
FileSize
2.61 KB

The drupal_find_theme_templates() uses the cache built up from all the modules implementation of hook_theme() but twig_theme() gets called before the theme's implementations of hook_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 last

lauriii’s picture

Issue tags: +Needs tests

It would be nice to have test coverage to prove this use case is working so that we don't break it in the future

joelpittet’s picture

Here's a test that demos the problem.

The last submitted patch, 9: 2817103-9--tests-only-fail.patch, failed testing.

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

The 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.

+++ b/core/modules/system/tests/themes/test_theme/test_theme.theme
@@ -6,6 +6,19 @@
+    'skip_rope' => [

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.

joelpittet’s picture

Changed the theme hook name.

lauriii’s picture

Thanks @joelpittet. Still RTBC!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: 2817103-12.patch, failed testing.

joelpittet’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
4.64 KB
494 bytes

I guess now we know render element is default for hooks. Adding variables key back in passes tests.

Back to RTBC there isn't anything different in the patch besides the lack of that key.

xjm’s picture

Version: 8.2.x-dev » 8.3.x-dev
Assigned: Unassigned » star-szr
Issue tags: +D8 major triage deferred, +Contributed project blocker
+++ b/core/includes/theme.inc
@@ -240,8 +240,7 @@ function drupal_find_theme_templates($cache, $extension, $path) {
-        $template_candidates = array($info['template'], str_replace($info['theme path'] . '/templates/', '', $info['template']));
-        if (in_array($template, $template_candidates)) {
+        if ($template == $info['template']) {

Per @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.

joelpittet’s picture

Removed 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

lauriii’s picture

Assigned: star-szr » lauriii
Status: Reviewed & tested by the community » Needs work

I'm working on addressing concerns that @Cottser might have by adding a new post_theme hook for the theme engines for this purpose.

lauriii’s picture

Assigned: lauriii » Unassigned
Status: Needs work » Needs review
FileSize
4.42 KB
3.93 KB

Let's see if this works

joelpittet’s picture

@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:

  1. modules
  2. engines
  3. themes

to

  1. modules
  2. themes
  3. engines

What are the other changes and why are they needed?

dawehner’s picture

+++ b/core/lib/Drupal/Core/Theme/Registry.php
@@ -348,6 +348,17 @@ protected function build() {
+    // Trigger post theme build hooks for the theme engines.

We should document why we are doing that/why this is needed

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Reposting my patch from #17 because of what I mentioned in ##2 and #19 introduced an API change with changing twig_theme to twig_theme_post_build

@lauriii please explain the changes further before reposting #19

joelpittet’s picture

The last submitted patch, 23: theme_registry_doesn_t-2817103-23.patch, failed testing.

lauriii’s picture

Two reasons why I think we should take the approach from #19:

  1. It makes conceptually sense to first process theme engine vs. theme. The loading order goes from higher level extensions to lower level extensions. Themes can depend on theme engines but not the other way around. This is the same approach as we've taken with hooks as well. The fix for this shouldn't be making an exception for the loading order because we want to do conceptually something new; we want the theme engine to be able to be able to make modifications in the post-process phase (when all theme implementations have been added).
    • modules
    • engines
    • themes
  2. The even more worrysome issue is that approach taken on #17 would break BC. If anyone has built custom theme engine, and a theme that depends on the logic in the theme engine, we wouldn't only break this, but make it impossible to tackle, since there wouldn't be any hook available for the theme engine to make modifications to theme hooks before the theme.

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.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

cameron prince’s picture

Re-rolled against 8.5.x.

Status: Needs review » Needs work

The last submitted patch, 28: theme_registry_doesn_t-2817103-28.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

ciss’s picture

Confirming 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).

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

B-Prod’s picture

Re-roll of the patch for D9.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

xjm’s picture

Issue tags: -D8 major triage deferred

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.