Score:2

Why does Drupal >= 9.4.7 not allow including .php files in Twig templates?

do flag

I have been handed a site with an old custom theme purchased from a third party.

After the last drupal update - 9.4.8 - logged in users stopped being able to access the front end of the site. Administration pages were still accessible.

After some digging I found the error was this line in a twig template:

{% include directory ~ '/customize/form.php' %}

It was causing the following error:

Twig\Error\LoaderError: Template "themes/gavias_tico/customize/form.php" is not defined in "themes/gavias_tico/templates/addon/skins.html.twig" at line 4. in Twig\Loader\ChainLoader->getCacheKey() (line 98 of /var/www/mysite.org.au/vendor/twig/twig/src/Loader/ChainLoader.php).

The template (php) file is present at the location listed so it appears twig is finding the correct path, however, the site is still WoD after this for logged-in users.

Has there been a change to the way twig includes should be done in drupal? How can I fix this?

Kevin avatar
in flag
Why would you include .php files in twig?
do flag
I wouldn't. I'm fixing someone else's codebase.
do flag
Further investigation makes me think the original theme authors were updating an old drupal 7 theme to work with d8/twig and rather than re-write everything they decided to import snippets of old template code written in php (it's mostly just html) into minimal twig files for compatibility.
Antoine Holman avatar
us flag
Were you able to discover a solution? Did you rewrite everything, scrap the theme, or did you find a fix?
do flag
I renamed all the imported files and import statements. Luckily for me the problem files mostly just contained html. If you are dealing with old embedded php the re-factoring process could be more complex.
Score:5
id flag

This is from Drupal 9.4.7 and its release notes explain:

“Following this release, by default, Twig may load the following file types:

  • .css
  • .html
  • .js
  • .svg
  • .twig

If your site, module, or theme must load additional file types via Twig, consult the documentation for twig.config.allowed_file_extensions in default.services.yml.”

But allowing other types may be dangerous. Read the security advisory for more information.

Score:2
sa flag

I faced an same type of issue when updating the Drupal core from 9.4.5 to 9.4.8

Twig\Error\LoaderError: Template "themes/gavias_tico/customize/form.php" is not defined in "themes/gavias_tico/templates/addon/skins.html.twig" at line 4. in Twig\Loader\ChainLoader->getCacheKey() (line 98 of /var/www/mysite.org.au/vendor/twig/twig/src/Loader/ChainLoader.php).

Based on the @cilefe, due to the security advisory only the following extensions are allowed to use in twig fix.

.css .html .js .svg .twig

Using php file in twig file is not allowed. I just changed the name of the file from yourfilename.php to yourfilename.html, and the same file called in the skins.html.twig

Hope this could helps to sort out this issue

do flag
Yes, that is more-or-less how I fixed the issue. But if you are dealing with a more complex set of old theme files you might have to re-factor the actual files themselves. Since my question is about import behaviour i think @cilefen is the right answer and best starting point for other people with the issue. You're solution does work in many circumstances - including mine - though.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.