From 4deadaa093ba8a66dd62f574e3517960def0cbee Mon Sep 17 00:00:00 2001 From: nod_ <nod_@598310.no-reply.drupal.org> Date: Wed, 14 Aug 2024 21:19:19 +0200 Subject: [PATCH] Issue #3451667 by jasonawant, pooja_sharma, samit.310@gmail.com, catch, longwave: AssetResolver::getCssAssets $cid does not account for ckeditor5-stylesheets variations --- core/core.services.yml | 2 +- core/lib/Drupal/Core/Asset/AssetResolver.php | 30 +++++++++++++++---- .../Tests/Core/Asset/AssetResolverTest.php | 11 ++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index cc45b9132343..8b9d1119c300 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1703,7 +1703,7 @@ services: Drupal\Core\Asset\LibraryDependencyResolverInterface: '@library.dependency_resolver' asset.resolver: class: Drupal\Core\Asset\AssetResolver - arguments: ['@library.discovery', '@library.dependency_resolver', '@module_handler', '@theme.manager', '@language_manager', '@cache.data'] + arguments: ['@library.discovery', '@library.dependency_resolver', '@module_handler', '@theme.manager', '@language_manager', '@cache.data', '@theme_handler'] Drupal\Core\Asset\AssetResolverInterface: '@asset.resolver' info_parser: class: Drupal\Core\Extension\InfoParser diff --git a/core/lib/Drupal/Core/Asset/AssetResolver.php b/core/lib/Drupal/Core/Asset/AssetResolver.php index fcd294a64971..4d42f4235984 100644 --- a/core/lib/Drupal/Core/Asset/AssetResolver.php +++ b/core/lib/Drupal/Core/Asset/AssetResolver.php @@ -7,6 +7,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Extension\ThemeHandlerInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Theme\ThemeManagerInterface; @@ -58,6 +59,13 @@ class AssetResolver implements AssetResolverInterface { */ protected $cache; + /** + * The theme handler service. + * + * @var \Drupal\Core\Extension\ThemeHandlerInterface + */ + protected $themeHandler; + /** * Constructs a new AssetResolver instance. * @@ -73,14 +81,22 @@ class AssetResolver implements AssetResolverInterface { * The language manager. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * The cache backend. + * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler + * The theme handler service. */ - public function __construct(LibraryDiscoveryInterface $library_discovery, LibraryDependencyResolverInterface $library_dependency_resolver, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, LanguageManagerInterface $language_manager, CacheBackendInterface $cache) { + public function __construct(LibraryDiscoveryInterface $library_discovery, LibraryDependencyResolverInterface $library_dependency_resolver, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, LanguageManagerInterface $language_manager, CacheBackendInterface $cache, ?ThemeHandlerInterface $theme_handler = NULL) { + if ($theme_handler === NULL) { + @trigger_error('Calling ' . __METHOD__ . ' without the $theme_handler argument is deprecated in drupal:11.1.0 and it will be required in drupal:12.0.0. See https://www.drupal.org/project/drupal/issues/3451667', E_USER_DEPRECATED); + $theme_handler = \Drupal::service('theme_handler'); + } + $this->libraryDiscovery = $library_discovery; $this->libraryDependencyResolver = $library_dependency_resolver; $this->moduleHandler = $module_handler; $this->themeManager = $theme_manager; $this->languageManager = $language_manager; $this->cache = $cache; + $this->themeHandler = $theme_handler; } /** @@ -140,10 +156,14 @@ public function getCssAssets(AttachedAssetsInterface $assets, $optimize, ?Langua if (!isset($language)) { $language = $this->languageManager->getCurrentLanguage(); } - $theme_info = $this->themeManager->getActiveTheme(); - // Add the theme name to the cache key since themes may implement - // hook_library_info_alter(). - $cid = 'css:' . $theme_info->getName() . ':' . $language->getId() . Crypt::hashBase64(serialize($libraries_to_load)) . (int) $optimize; + // Add the active theme name to the cache key since active themes may + // implement hook_library_info_alter(). + $active_theme = $this->themeManager->getActiveTheme()->getName(); + // Add the default theme name to the cache key since css generated for an + // active admin theme may include the default theme's ckeditor5-stylesheets + // and default themes may be set conditionally and dynamically. + $default_theme = $this->themeHandler->getDefault(); + $cid = 'css:' . $active_theme . ':' . $default_theme . ':' . $language->getId() . Crypt::hashBase64(serialize($libraries_to_load)) . (int) $optimize; if ($cached = $this->cache->get($cid)) { return $cached->data; } diff --git a/core/tests/Drupal/Tests/Core/Asset/AssetResolverTest.php b/core/tests/Drupal/Tests/Core/Asset/AssetResolverTest.php index d5e2190439a5..4c5b49287d59 100644 --- a/core/tests/Drupal/Tests/Core/Asset/AssetResolverTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/AssetResolverTest.php @@ -67,6 +67,13 @@ class AssetResolverTest extends UnitTestCase { */ protected $cache; + /** + * The mocked theme handler. + * + * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit\Framework\MockObject\MockObject + */ + protected $themeHandler; + /** * A mocked English language object. */ @@ -161,7 +168,9 @@ protected function setUp(): void { ->willReturn($english, $english, $japanese, $japanese); $this->cache = new TestMemoryBackend(new Time()); - $this->assetResolver = new AssetResolver($this->libraryDiscovery, $this->libraryDependencyResolver, $this->moduleHandler, $this->themeManager, $this->languageManager, $this->cache); + $this->themeHandler = $this->createMock('\Drupal\Core\Extension\ThemeHandlerInterface'); + + $this->assetResolver = new AssetResolver($this->libraryDiscovery, $this->libraryDependencyResolver, $this->moduleHandler, $this->themeManager, $this->languageManager, $this->cache, $this->themeHandler); } /** -- GitLab