From 2765ec28e855589fd6f70ead203a33548e99ef20 Mon Sep 17 00:00:00 2001
From: Pjotr Savitski <pjotr.savitski@gmail.com>
Date: Tue, 8 Aug 2023 12:48:09 +0300
Subject: [PATCH] Fixed an issue with optimizing JS and CSS assets.

Defined two optimization services based on now deprecated classes, changed the code to use those services and made a few more changes to make it work as expected. Added a few changes to license definitions to remove missing key warnings.
---
 h5p.module                                | 4 ++++
 h5p.services.yml                          | 7 +++++++
 modules/h5peditor/h5peditor.libraries.yml | 5 +++--
 src/H5PDrupal/H5PDrupal.php               | 9 +++++----
 4 files changed, 19 insertions(+), 6 deletions(-)
 create mode 100644 h5p.services.yml

diff --git a/h5p.module b/h5p.module
index 10b6449..f9e7994 100644
--- a/h5p.module
+++ b/h5p.module
@@ -50,6 +50,7 @@ function h5p_library_info_build() {
     'version' => '1.0',
     'license' => [
       'name' => 'MIT',
+      'url' => 'https://h5p.org',
       'gpl-compatible' => TRUE,
     ],
     'js' => [
@@ -94,6 +95,7 @@ function h5p_library_info_build() {
     'version' => '1.0',
     'license' => [
       'name' => 'MIT',
+      'url' => 'https://h5p.org',
       'gpl-compatible' => TRUE,
     ],
     'css' => [
@@ -117,6 +119,7 @@ function h5p_library_info_build() {
     'version' => '1.0',
     'license' => [
       'name' => 'MIT',
+      'url' => 'https://h5p.org',
       'gpl-compatible' => TRUE,
     ],
     'js' => [
@@ -131,6 +134,7 @@ function h5p_library_info_build() {
     'version' => '1.0',
     'license' => [
       'name' => 'MIT',
+      'url' => 'https://h5p.org',
       'gpl-compatible' => TRUE,
     ],
     'js' => [
diff --git a/h5p.services.yml b/h5p.services.yml
new file mode 100644
index 0000000..23c9019
--- /dev/null
+++ b/h5p.services.yml
@@ -0,0 +1,7 @@
+services:
+  asset.css.collection_optimizer_legacy:
+    class: Drupal\Core\Asset\CssCollectionOptimizer
+    arguments: [ '@asset.css.collection_grouper', '@asset.css.optimizer', '@asset.css.dumper', '@state', '@file_system' ]
+  asset.js.collection_optimizer_legacy:
+    class: Drupal\Core\Asset\JsCollectionOptimizer
+    arguments: [ '@asset.js.collection_grouper', '@asset.js.optimizer', '@asset.js.dumper', '@state', '@file_system' ]
diff --git a/modules/h5peditor/h5peditor.libraries.yml b/modules/h5peditor/h5peditor.libraries.yml
index 1d8b8e4..19246e6 100644
--- a/modules/h5peditor/h5peditor.libraries.yml
+++ b/modules/h5peditor/h5peditor.libraries.yml
@@ -1,8 +1,9 @@
 h5peditor:
   version: 1.0
   license:
-    - name: MIT
-    - gpl-compatible: TRUE
+    name: MIT
+    url: https://h5p.org
+    gpl-compatible: TRUE
   js:
     scripts/application.js: {}
   dependencies:
diff --git a/src/H5PDrupal/H5PDrupal.php b/src/H5PDrupal/H5PDrupal.php
index 14b4a98..d61b212 100644
--- a/src/H5PDrupal/H5PDrupal.php
+++ b/src/H5PDrupal/H5PDrupal.php
@@ -194,11 +194,11 @@ class H5PDrupal implements \H5PFrameworkInterface {
    *   Stylesheet assets.
    */
   public static function aggregatedAssets($scriptAssets, $styleAssets) {
-    $jsOptimizer = \Drupal::service('asset.js.collection_optimizer');
-    $cssOptimizer = \Drupal::service('asset.css.collection_optimizer');
+    $jsOptimizer = \Drupal::service('asset.js.collection_optimizer_legacy');
+    $cssOptimizer = \Drupal::service('asset.css.collection_optimizer_legacy');
     $systemPerformance = \Drupal::config('system.performance');
     $jsAssetConfig = ['preprocess' => $systemPerformance->get('js.preprocess')];
-    $cssAssetConfig = ['preprocess' => $systemPerformance->get('css.preprocess'), 'media' => 'css'];
+    $cssAssetConfig = ['preprocess' => $systemPerformance->get('css.preprocess'), 'media' => 'all'];
     $assets = ['scripts' => [], 'styles' => []];
     foreach ($scriptAssets as $jsFiles) {
       $assets['scripts'][] = self::createCachedPublicFiles($jsFiles, $jsOptimizer, $jsAssetConfig);
@@ -228,6 +228,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
       'attributes' => [],
       'version' => NULL,
       'browsers' => [],
+      'license' => FALSE,
     ];
 
     foreach ($filePaths as $index => $path) {
@@ -238,7 +239,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
         'data' => $path,
       ] + $assetConfig + $defaultAssetConfig;
     }
-    $cachedAsset = $optimizer->optimize($assets);
+    $cachedAsset = $optimizer->optimize($assets, []);
 
     return array_map(function($publicUrl){ return \Drupal::service('file_url_generator')->generateAbsoluteString($publicUrl); }, array_column($cachedAsset, 'data'));
   }
-- 
2.39.2 (Apple Git-143)