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

Comments

marcoscano created an issue. See original summary.

marcoscano’s picture

Issue summary: View changes
StatusFileSize
new176.81 KB
marcoscano’s picture

Title: Templates not found if deeply nested » Templates not found in some theme directory configurations
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new713 bytes

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

marcoscano’s picture

e0ipso’s picture

Status: Needs review » Reviewed & tested by the community

This is working good in our install. Thanks for the patch @marcoscano!

julio_retkwa’s picture

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

component-libraries:
  base:
    paths:
      - components/00-base
  atoms:
    paths:
      - components/01-atoms
  molecules:
    paths:
      - components/02-molecules
  organisms:
    paths:
      - components/03-organisms

To something like:

components:
  namespaces:
    base: components/00-base
    atoms: components/01-atoms
    molecules: components/02-molecules
    organisms: components/03-organisms
    templates: components/04-templates
    pages: components/05-pages
joycehutch’s picture

Thanks @julio_retkwa! I also had to change the theme.info.yml with the same, because of pattern lab configuration. This was helpful!

joelpittet’s picture

I wonder if a BC layer can be added to help ease people off of 2.x?

Thanks @julio_retkwa for the TL;DR

pasan.gamage’s picture

Thanks @julio_retkwa
Saved me a heap of time!

  • RobLoach committed 0b46c285 on 3.x
    Issue #3368589 by marcoscano, RobLoach: Trim the right-hand slash on...
robloach’s picture

Title: Templates not found in some theme directory configurations » Trim the right-hand slash on namespace paths
Status: Reviewed & tested by the community » Fixed
StatusFileSize
new713 bytes

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

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.