Problem/Motivation

Incorrect theme is loaded on admin paths when critical_css is enabled.

Steps to reproduce

Set seven or any theme as admin theme and particle or any other theme as default theme. Enable critical_css and go to admin pages and you will notice the your default theme is loading instead of your admin theme.

Proposed resolution

Prevent Drupal\Core\Theme\ThemeManager->getActiveTheme() from being called too early.

Issue fork components-3165589

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

majid.ali created an issue. See original summary.

majid.ali’s picture

Title: Incorrect theme loaded when critical_css module is intalled » Incorrect theme loaded when critical_css module is installed
johnalbin’s picture

Category: Bug report » Support request
Status: Needs work » Postponed (maintainer needs more info)

Looking at the current code in the critical_css module, it disables itself on admin pages. https://git.drupalcode.org/project/critical_css/-/blob/8.x-1.x/src/Asset...

So I don't think the issue description is accurate.

evaldas.uzkuras’s picture

StatusFileSize
new340.36 KB

The problem is that Drupal\components\Template\Loader\ComponentsLoader calls
Drupal\Core\Theme\ThemeManager->getActiveTheme() too early and
\Drupal\user\Theme\AdminNegotiator can't correctly determine admin theme because correct RouteMatch is not set yet.

The issue is caused by critical_css because it decorates `asset.css.collection_renderer` service (https://git.drupalcode.org/project/critical_css/-/blob/8.x-1.x/critical_...) and adds twig service as dependency, which later constructs `components.twig.loader` service.

`asset.css.collection_renderer` is used by `ajax_response.attachments_processor` which is used by `ajax_response.subscriber`;

The correct RouteMatch is set by `router_listener`, which has smaller priority than `ajax_response.subscriber`, and is called later (while the active theme is already set)
For this reason we get incorrect theme

Attached a screenshot to display whole chain of services involved.
di-chain-backtrace

johnalbin’s picture

Version: 8.x-2.x-dev » 3.x-dev
Category: Support request » Bug report
Issue summary: View changes
Status: Postponed (maintainer needs more info) » Active

Thanks, Evaldas! That's super helpful info.

The problem is that Drupal\components\Template\Loader\ComponentsLoader calls
Drupal\Core\Theme\ThemeManager->getActiveTheme() too early

I think this is exactly the cause of the bug.

I looked at how Drupal core's ThemeRegistryLoader works in detail and it causes Drupal\Core\Theme\ThemeManager->getActiveTheme() to be called too, but it doesn't do so during its constructor. It only triggers that method in ThemeRegistryLoader::findTemplate(). We should do the same in ComponentsLoader.

This bug exists in both 3.x and 8.x-2.x.

johnalbin’s picture

Status: Active » Needs review
johnalbin’s picture

Status: Needs review » Fixed

Fixed in 3.x.

johnalbin’s picture

Version: 3.x-dev » 8.x-2.x-dev
Status: Fixed » Patch (to be ported)

This needs to be backported to 8.x-2.x.

johnalbin’s picture

Status: Patch (to be ported) » Needs review

  • JohnAlbin committed 1a5e504 on 8.x-2.x
    Issue #3165589 by JohnAlbin, majid.ali, Evaldas Užkuras: Incorrect theme...
johnalbin’s picture

Status: Needs review » Fixed

Evaldas and Majid,

Can you check out the latest code on the 8.x-2.x branch and see if that fixes the bug? I haven't tested my fix against the critical_css module.

If the bug is fixed, I can make a new 8.x-2.2 release with the bugfix.

evaldas.uzkuras’s picture

John,

2.x still not work correctly.

There is one more part that calls getActiveTheme (introduced with #3191968: Add alter hook for protected Twig namespaces in 2.1.0)

Drupal\components\Template\ComponentsInfo is calling alter for ThemeManager on construct ( $themeManager->alter('protected_twig_namespaces', $this->protectedNamespaces) ) which is calling getActiveTheme.

johnalbin’s picture

Version: 8.x-2.x-dev » 3.x-dev
Assigned: Unassigned » johnalbin
Status: Fixed » Needs work

Drupal\components\Template\ComponentsInfo is calling alter for ThemeManager on construct ( $themeManager->alter('protected_twig_namespaces', $this->protectedNamespaces) ) which is calling getActiveTheme.

*sigh* ok. Thanks for the new info! It really helps a lot.

So we'll need to add an initialization flag to ComponentsInfo and not init() the namespaces data until ComponentsLoader::findTemplate() is called.

  • JohnAlbin committed ef68840 on 3.x
    Issue #3165589 by JohnAlbin, majid.ali, Evaldas Užkuras: Incorrect theme...

johnalbin’s picture

Status: Needs work » Needs review

  • JohnAlbin committed 8407464 on 3.x
    Issue #3165589 by JohnAlbin, majid.ali, Evaldas Užkuras: Incorrect theme...
johnalbin’s picture

Assigned: johnalbin » Unassigned
Status: Needs review » Fixed

johnalbin’s picture

Version: 3.x-dev » 8.x-2.x-dev
Status: Fixed » Needs review

  • JohnAlbin committed 173f4bf on 8.x-2.x
    Issue #3165589 by JohnAlbin, majid.ali, Evaldas Užkuras: Incorrect theme...
johnalbin’s picture

Status: Needs review » Fixed

Ok. One more time.

Can you check out the latest code on the 8.x-2.x branch and see if that fixes the bug?

If the bug is fixed, I can make a new 8.x-2.2 release with the bugfix.

evaldas.uzkuras’s picture

It's perfect now, the correct admin theme is activated.

johnalbin’s picture

It's perfect now, the correct admin theme is activated.

Thanks for all your help, Evaldas!

https://www.drupal.org/project/components/releases/8.x-2.2

Status: Fixed » Closed (fixed)

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