'directory' variable statically cached makes active theme override incomplete in some case

Created on 27 August 2019, almost 6 years ago
Updated 29 July 2025, 10 days ago

Problem/Motivation

Given :

  1. A backend form
  2. Containing a Managed file form element, containing at least one file on submission
  3. Sending an e-mail on submit (containing the uploaded files as attachments for exemple)
  4. E-mail has to use another theme than the backend (active) one.
  5. {{ directory }} variable is used in the e-mail template (for example to generate absolute paths to images)

The active theme can't be fully overriden (for example using \Drupal::theme()->setActiveTheme()) because :

  1. \Drupal\file\Element\ManagedFile::processManagedFile() load the list of files during the page load before form submission
  2. $variables['directory'] is statically cached during the page load by _template_preprocess_default_variables() (through the template_preprocess() function)

So the {{ directory }} variable in the final rendering get the path from the theme of the form, not from the override. It doesn't reflect the actually active theme, this variable is not reliable.

We don't get this behavior if the managed file field is empty, because the list of links is empty so not rendered.

Proposed resolution

Initialize $variables['directory'] as empty in template_preprocess() and set it to the active theme out of the template_preprocess() scope (for example in \Drupal\Core\Theme\ThemeManager::render() where $variables['directory'] is used as a flag) .

Remaining tasks

To be determined.

User interface changes

No user interface change to expect.

API changes

No API change to expect.

Data model changes

No data model change to expect.

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

file system

Created by

πŸ‡«πŸ‡·France mattew

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024