Loading core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php +10 −16 Original line number Diff line number Diff line Loading @@ -322,25 +322,21 @@ public function libraryInfoAlter(&$libraries, $extension): void { */ #[Hook('js_alter')] public function jsAlter(&$javascript, AttachedAssetsInterface $assets, LanguageInterface $language): void { $translations_library = 'core/ckeditor5.translations'; // This file means CKEditor 5 translations are in use on the page. // @see locale_js_alter() $placeholder_file = 'core/assets/vendor/ckeditor5/translation.js'; if (in_array($translations_library, $assets->getLibraries(), TRUE) || in_array($translations_library, $assets->getAlreadyLoadedLibraries(), TRUE)) { // This file is used to get a weight that will make it possible to // aggregate all translation files in a single aggregate. // This file is used to get a weight that will make it possible to aggregate // all translation files in a single aggregate. $ckeditor_dll_file = 'core/assets/vendor/ckeditor5/ckeditor5-dll/ckeditor5-dll.js'; if (isset($javascript[$placeholder_file])) { // Use the placeholder file weight to set all the translations files // weights so they can be aggregated together as expected. Account for // requests where the library is not loaded such as when during an AJAX // request when it was already loaded via the main request. In these cases // it is unlikely that multiple JavaScript aggregates will be created // anyway since AJAX requests generally result in very few libraries being // loaded. $default_weight = $javascript[$placeholder_file]['weight'] ?? 0; // weights so they can be aggregated together as expected. $default_weight = $javascript[$placeholder_file]['weight']; if (isset($javascript[$ckeditor_dll_file])) { $default_weight = $javascript[$ckeditor_dll_file]['weight']; } // The placeholder file is not a real file, remove it from the list. unset($javascript[$placeholder_file]); // When the locale module isn't installed there are no translations. if (!\Drupal::moduleHandler()->moduleExists('locale')) { return; Loading @@ -365,8 +361,6 @@ public function jsAlter(&$javascript, AttachedAssetsInterface $assets, LanguageI } } } // The placeholder file is not a real file, remove it from the list. unset($javascript[$placeholder_file]); } /** Loading core/modules/ckeditor5/tests/src/Unit/CKEditor5Test.php +0 −77 Original line number Diff line number Diff line Loading @@ -4,13 +4,7 @@ namespace Drupal\Tests\ckeditor5\Unit; use Drupal\ckeditor5\LanguageMapper; use Drupal\ckeditor5\Hook\Ckeditor5Hooks; use Drupal\ckeditor5\Plugin\Editor\CKEditor5; use Drupal\Core\Asset\AttachedAssets; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\Language; use Drupal\Tests\ckeditor5\Traits\PrivateMethodUnitTestTrait; use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\Attributes\CoversClass; Loading Loading @@ -114,75 +108,4 @@ public static function providerPathsToFormNames(): array { ]; } /** * Test the js_alter hook alters when expected. * * @legacy-covers \Drupal\ckeditor5\Hook\Ckeditor5Hooks::jsAlter */ public function testJsAlterHook(): void { $placeholder_file = 'core/assets/vendor/ckeditor5/translation.js'; $language_mapper = $this->createMock(LanguageMapper::class); $language_mapper->expects($this->any()) ->method('getMapping') ->willReturn('en'); $hooks = new Ckeditor5Hooks($language_mapper); $assets = new AttachedAssets(); $assets->setLibraries([ 'core/ckeditor5.translations', 'core/ckeditor5.translations.en', ]); $language = new Language([ 'id' => 'en', 'name' => 'English', 'direction' => 'ltr', ]); $original_javascript = [ 'keep_this' => [ 'ckeditor5_langcode' => 'en', ], 'remove_this' => [ 'ckeditor5_langcode' => 'sv', ], 'keep_this_too' => [], ]; $expected_javascript = [ 'keep_this' => [ 'ckeditor5_langcode' => 'en', 'weight' => 5, ], 'keep_this_too' => [], ]; $container = new ContainerBuilder(); $module_handler = $this->prophesize(ModuleHandlerInterface::class); $module_handler->moduleExists('locale')->willReturn(TRUE); $container->set('module_handler', $module_handler->reveal()); \Drupal::setContainer($container); // First check that it filters when the placeholder script is present. $javascript = $original_javascript + [ $placeholder_file => [ 'weight' => 5, ], ]; $hooks->jsAlter($javascript, $assets, $language); $this->assertEquals($expected_javascript, $javascript); // Next check it still filters if the placeholder script has already been // loaded and is now excluded from the list, such as an AJAX operation // loading a new format which uses another set of plugins. $assets = new AttachedAssets(); $assets->setLibraries([ 'core/ckeditor5.translations.en', ]); $assets->setAlreadyLoadedLibraries([ 'core/ckeditor5.translations', ]); $javascript = $original_javascript; $hooks->jsAlter($javascript, $assets, $language); // There was no placeholder to get the weight from. $expected_javascript['keep_this']['weight'] = 0; $this->assertEquals($expected_javascript, $javascript); } } Loading
core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php +10 −16 Original line number Diff line number Diff line Loading @@ -322,25 +322,21 @@ public function libraryInfoAlter(&$libraries, $extension): void { */ #[Hook('js_alter')] public function jsAlter(&$javascript, AttachedAssetsInterface $assets, LanguageInterface $language): void { $translations_library = 'core/ckeditor5.translations'; // This file means CKEditor 5 translations are in use on the page. // @see locale_js_alter() $placeholder_file = 'core/assets/vendor/ckeditor5/translation.js'; if (in_array($translations_library, $assets->getLibraries(), TRUE) || in_array($translations_library, $assets->getAlreadyLoadedLibraries(), TRUE)) { // This file is used to get a weight that will make it possible to // aggregate all translation files in a single aggregate. // This file is used to get a weight that will make it possible to aggregate // all translation files in a single aggregate. $ckeditor_dll_file = 'core/assets/vendor/ckeditor5/ckeditor5-dll/ckeditor5-dll.js'; if (isset($javascript[$placeholder_file])) { // Use the placeholder file weight to set all the translations files // weights so they can be aggregated together as expected. Account for // requests where the library is not loaded such as when during an AJAX // request when it was already loaded via the main request. In these cases // it is unlikely that multiple JavaScript aggregates will be created // anyway since AJAX requests generally result in very few libraries being // loaded. $default_weight = $javascript[$placeholder_file]['weight'] ?? 0; // weights so they can be aggregated together as expected. $default_weight = $javascript[$placeholder_file]['weight']; if (isset($javascript[$ckeditor_dll_file])) { $default_weight = $javascript[$ckeditor_dll_file]['weight']; } // The placeholder file is not a real file, remove it from the list. unset($javascript[$placeholder_file]); // When the locale module isn't installed there are no translations. if (!\Drupal::moduleHandler()->moduleExists('locale')) { return; Loading @@ -365,8 +361,6 @@ public function jsAlter(&$javascript, AttachedAssetsInterface $assets, LanguageI } } } // The placeholder file is not a real file, remove it from the list. unset($javascript[$placeholder_file]); } /** Loading
core/modules/ckeditor5/tests/src/Unit/CKEditor5Test.php +0 −77 Original line number Diff line number Diff line Loading @@ -4,13 +4,7 @@ namespace Drupal\Tests\ckeditor5\Unit; use Drupal\ckeditor5\LanguageMapper; use Drupal\ckeditor5\Hook\Ckeditor5Hooks; use Drupal\ckeditor5\Plugin\Editor\CKEditor5; use Drupal\Core\Asset\AttachedAssets; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\Language; use Drupal\Tests\ckeditor5\Traits\PrivateMethodUnitTestTrait; use Drupal\Tests\UnitTestCase; use PHPUnit\Framework\Attributes\CoversClass; Loading Loading @@ -114,75 +108,4 @@ public static function providerPathsToFormNames(): array { ]; } /** * Test the js_alter hook alters when expected. * * @legacy-covers \Drupal\ckeditor5\Hook\Ckeditor5Hooks::jsAlter */ public function testJsAlterHook(): void { $placeholder_file = 'core/assets/vendor/ckeditor5/translation.js'; $language_mapper = $this->createMock(LanguageMapper::class); $language_mapper->expects($this->any()) ->method('getMapping') ->willReturn('en'); $hooks = new Ckeditor5Hooks($language_mapper); $assets = new AttachedAssets(); $assets->setLibraries([ 'core/ckeditor5.translations', 'core/ckeditor5.translations.en', ]); $language = new Language([ 'id' => 'en', 'name' => 'English', 'direction' => 'ltr', ]); $original_javascript = [ 'keep_this' => [ 'ckeditor5_langcode' => 'en', ], 'remove_this' => [ 'ckeditor5_langcode' => 'sv', ], 'keep_this_too' => [], ]; $expected_javascript = [ 'keep_this' => [ 'ckeditor5_langcode' => 'en', 'weight' => 5, ], 'keep_this_too' => [], ]; $container = new ContainerBuilder(); $module_handler = $this->prophesize(ModuleHandlerInterface::class); $module_handler->moduleExists('locale')->willReturn(TRUE); $container->set('module_handler', $module_handler->reveal()); \Drupal::setContainer($container); // First check that it filters when the placeholder script is present. $javascript = $original_javascript + [ $placeholder_file => [ 'weight' => 5, ], ]; $hooks->jsAlter($javascript, $assets, $language); $this->assertEquals($expected_javascript, $javascript); // Next check it still filters if the placeholder script has already been // loaded and is now excluded from the list, such as an AJAX operation // loading a new format which uses another set of plugins. $assets = new AttachedAssets(); $assets->setLibraries([ 'core/ckeditor5.translations.en', ]); $assets->setAlreadyLoadedLibraries([ 'core/ckeditor5.translations', ]); $javascript = $original_javascript; $hooks->jsAlter($javascript, $assets, $language); // There was no placeholder to get the weight from. $expected_javascript['keep_this']['weight'] = 0; $this->assertEquals($expected_javascript, $javascript); } }