Problem/Motivation
This was discovered over at #3301173: Allow starterkit theme generator tool to clone Olivero.
If a theme is enabled in the UI, any SDC components inside that new theme are not available unless you additionally do a Drupal cache clear. This causes Drupal to throw a 500 error to the admin user who enabled the theme! This isn't an issue for components in modules, since ModuleInstaller does clear those caches, while ThemeInstaller does not.
Steps to reproduce
Proposed resolution
I grabbed this from ModuleInstaller and it solves the issue. But I have no idea yet if it's the correct fix.
diff --git a/core/lib/Drupal/Core/Extension/ThemeInstaller.php b/core/lib/Drupal/Core/Extension/ThemeInstaller.php
index 364d672c12f..9565fcf37b4 100644
--- a/core/lib/Drupal/Core/Extension/ThemeInstaller.php
+++ b/core/lib/Drupal/Core/Extension/ThemeInstaller.php
@@ -248,6 +248,7 @@ public function install(array $theme_list, $install_dependencies = TRUE) {
}
$this->cssCollectionOptimizer->deleteAll();
+ \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
$this->resetSystem();
// Invoke hook_themes_installed() after the themes have been installed.
@@ -301,6 +302,7 @@ public function uninstall(array $theme_list) {
$extension_config->save(TRUE);
// Refresh theme info.
+ \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
$this->resetSystem();
$this->themeHandler->reset();
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Comments
Comment #2
quietone commentedComment #3
mherchelBumping this to Major (note it may need to be critical). Most new themes will have SDCs, and this will affect anyone who install new themes via the UI.
Comment #5
mherchelSlack discussion at https://drupal.slack.com/archives/C079NQPQUEN/p1746998199858839
In it, backend framework manager @larowlan states (in regards to @deviantintegral approach in the issue summary)
Comment #6
mherchelUpdating the steps to reproduce
Comment #7
mherchelevidently the test case isn't as simple as I initially thought. I've run into this a lot, but need to see what the differentiator is that causes it.
Comment #8
godotislateHere's a failing kernel test that shows a couple issues going on.
First thing is that because there is no container rebuild or component plugin cache clear on theme install,
$definitionsget cached in the first line before the theme is installed, and the cache is not invalidated after theme install, so the last line's assert fails.However, even if we just add a plugin cache clear after the theme install like this:
The last line still fails, because there is no container rebuild on theme install, so the component plugin manager has not changed, and the DirectoryWithMetadataPluginDiscovery
$discoveryobject property in the component plugin manager does not get its$directoriesproperty updated to include the new theme's component directory. So as long as the component plugin definitions aren't cached again before the next request, the component plugins should be updated correctly, which can be seen if with the test rewritten to pass like this:So I think there are two things going on:
Comment #9
nicxvan commentedI think @godot is on to something. I pushed up a test case based on the original report and could not reproduce it manually or in the test.
Comment #11
godotislateMR with fix and test is up. I injected the component plugin manager instead of the plugin cache clearer into the theme installer, since it seemed to be overkill to clear all the plugin caches, but should be easy enough to change if needed.
Comment #13
deviantintegral commentedThanks for following up!
Comment #14
mherchelHeck yes! This totally fixes the issue for me. I'm attaching two videos:
Still leaving at NR, since we need a code review and I'm not super qualified for this level of PHP. But confirmed to fix the issue!
Comment #15
penyaskitoComment #16
godotislateComment #18
quietone commentedI read the comments in the MR and everything reads well. I updated credit.
Comment #19
larowlanOne comment about the deprecation notice
Fine to self RTBC after that change
Comment #20
acbramley commentedComment #23
larowlanCommitted to 11.x and 11.2.x
Published change record, thanks folks