Problem/Motivation
It seems like #3191392: Make subdirectory path optional when using @namespace/template syntax introduced a bug in certain scenarios.
In our project, bumping from 2.2.0 to 3.0.0-beta3 broke several pages, with errors such as:
Twig\Error\LoaderError: Template "@templates/node/node--layout--movies-hub.twig" is not defined in "sites/****/themes/custom/****/templates/node/full/node--layout--movies-hub.html.twig" at line 1. in Twig\Loader\ChainLoader->getCacheKey() (line 98 of /var/www/html/vendor/twig/twig/src/Loader/ChainLoader.php).
Twig\Environment->getTemplateClass('@templates/node/node--layout--movies-hub.twig', NULL) (Line: 204)
Drupal\Core\Template\TwigEnvironment->getTemplateClass('@templates/node/node--layout--movies-hub.twig') (Line: 381)
Twig\Environment->loadTemplate('@templates/node/node--layout--movies-hub.twig', NULL) (Line: 333)
Twig\Template->loadTemplate('@templates/node/node--layout--movies-hub.twig', 'sites/****/themes/custom/****/templates/node/full/node--layout--movies-hub.html.twig', 1) (Line: 39)
__TwigTemplate_88d77c497f448d9b8b0a12ac99839522105af94ff250d171bac18ca30c7ff718->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('sites/****/themes/custom/****/templates/node/full/node--layout--movies-hub.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('node', Array) (Line: 433)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 243)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 174)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Other issues seem to be reporting the same error after upgrading, such as:
#3362068: Upgrade from 2.x to 3.x breaks mail templates
#3353402: Upgrade to Components! 3.0.0-beta3 breaks website
although some of them might also include other issues due to DB updates not being run.
Steps to reproduce
I couldn't reliably reproduce the issue on a vanilla install, and need to switch to something else now. However, in a quick troubleshooting I did find that:
- If I remove the nested subdirectories from the include line in twig, things work fine. In other words, moving from:
include('@templates/node/node--layout--movies-hub.twig')
into
include('@templates/node--layout--movies-hub.twig')
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 3368589--trim-namespace-path-slash.patch | 713 bytes | robloach |
| #3 | 3368589-3.patch | 713 bytes | marcoscano |
Comments
Comment #2
marcoscanoComment #3
marcoscanoTurns out I was wrong suspecting from the long paths, that was my IDE playing a trick with me. Turns out the keys were fine and not trimmed at all.
On the other hand, I did find that sometimes (I'm guessing when the namespace pointed to a custom site-specific (in a multi-site) theme directory) we had a trailing slash, which made the
str_replace()not do the job it was supposed to do.Attached patch that seems to solve the issue for us.
Comment #4
marcoscanoComment #5
e0ipsoThis is working good in our install. Thanks for the patch @marcoscano!
Comment #6
julio_retkwa#3 working fine for me as I had to follow the new standard described here: https://www.drupal.org/docs/contributed-modules/components/registering-t...
TL:DR
I had to replace my theme.info.yml section from this:
To something like:
Comment #7
joycehutch commentedThanks @julio_retkwa! I also had to change the theme.info.yml with the same, because of pattern lab configuration. This was helpful!
Comment #8
joelpittetI wonder if a BC layer can be added to help ease people off of 2.x?
Thanks @julio_retkwa for the TL;DR
Comment #9
pasan.gamage commentedThanks @julio_retkwa
Saved me a heap of time!
Comment #11
robloachSmall fix to switch the string to use single-quotes instead of double quotes. We're not processing any variables in the string so ' is what we want. Thanks all!
Committed to 3.x.