Verified Commit 5254c779 authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #3432601 by catch, quietone, smustgrave: Add deprecation/bc support for...

Issue #3432601 by catch, quietone, smustgrave: Add deprecation/bc support for library-overrides when files are moved
parent e1e95969
Loading
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\Theme\ComponentPluginManager;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Plugin\Component;

@@ -420,6 +421,60 @@ protected function parseLibraryInfo($extension, $path) {
    return $libraries;
  }

  /**
   * Apply overrides to files that have moved.
   *
   * @param array $library
   *   The library definition.
   * @param string $library_name
   *   The library name.
   * @param string $extension
   *   The extension name.
   * @param array $overrides
   *   The library overrides.
   * @param Drupal\Core\Theme\ActiveTheme $active_theme
   *   The active theme.
   *
   * @return array
   *   The modified library overrides.
   */
  protected function applyLibrariesMovedOverrides(array $library, string $library_name, string $extension, array $overrides, ActiveTheme $active_theme): array {
    if (!isset($library['moved_files'])) {
      return $overrides;
    }
    foreach ($library['moved_files'] as $old_library_name => $moved_files) {
      $deprecation_version = $moved_files['deprecation_version'];
      $removed_version = $moved_files['removed_version'];
      $deprecation_link = $moved_files['deprecation_link'];
      if (isset($overrides[$old_library_name]['css']) && isset($moved_files['css'])) {
        foreach ($overrides[$old_library_name]['css'] as $key => $files) {
          foreach ($files as $original => $target) {
            if (isset($moved_files['css'][$key][$original])) {
              $new_key = array_key_first($moved_files['css'][$key][$original]);
              $new_file = $moved_files['css'][$key][$original][$new_key];
              $theme_name = $active_theme->getName();
              // phpcs:ignore
              @trigger_error("Targeting $old_library_name $original from $theme_name library_overrides is deprecated in $deprecation_version and will be removed in $removed_version. Target $extension/$library_name $new_file instead. See $deprecation_link", E_USER_DEPRECATED);
              $overrides[$extension . '/' . $library_name]['css'][$new_key][$new_file] = $target;
            }
          }
        }
      }
      if (isset($overrides[$old_library_name]['js']) && isset($moved_files['js'])) {
        foreach ($overrides[$old_library_name]['js'] as $original => $target) {
          if (isset($moved_files['js'][$original])) {
            $new_file = $moved_files['js'][$original];
            $theme_name = $active_theme->getName();
            // phpcs:ignore
            @trigger_error("Targeting $old_library_name $original from $theme_name library_overrides is deprecated in $deprecation_version and will be removed in $removed_version. Target $extension/$library_name $new_file instead. See $deprecation_link", E_USER_DEPRECATED);
            $overrides[$extension . '/' . $library_name]['js'][$new_file] = $target;
          }
        }
      }
    }
    return $overrides;
  }

  /**
   * Builds the dynamic library definitions for single directory components.
   *
@@ -469,6 +524,8 @@ protected function applyLibrariesOverride($libraries, $extension) {
    $all_libraries_overrides = $active_theme->getLibrariesOverride();
    foreach ($all_libraries_overrides as $theme_path => $libraries_overrides) {
      foreach ($libraries as $library_name => $library) {
        $libraries_overrides = $this->applyLibrariesMovedOverrides($library, $library_name, $extension, $libraries_overrides, $active_theme);

        // Process libraries overrides.
        if (isset($libraries_overrides["$extension/$library_name"])) {
          if (isset($library['deprecated'])) {
+25 −0
Original line number Diff line number Diff line
@@ -6,6 +6,31 @@ theme_stylesheets_override_and_remove_test:
      css/sub-override.css: {}
      css/sub-remove.css: {}

moved_to:
  version: VERSION
  css:
    base:
      css/base-remove.css: {}
  js:
    js/foo.js: {}
  moved_files:
    theme_test/moved_from:
      deprecation_version: drupal:X.0.0
      removed_version: drupal:Y.0.0
      deprecation_link: https://example.com
      css:
        base:
          css/foo.css:
            base: 'css/base-remove.css'
      js:
        js/bar.js: 'js/foo.js'

moved_from:
  version: VERSION
  css:
    base:
      css/sub-remove.css: {}

deprecated_library:
  version: VERSION
  css:
+6 −0
Original line number Diff line number Diff line
@@ -10,6 +10,12 @@ libraries-override:
    css:
      base:
        css/foo.css: css/bar.css
  theme_test/moved_from:
    css:
      base:
        css/foo.css: css/bar.css
    js:
      js/bar.js: false

libraries-extend:
  theme_test/another_deprecated_library:
+4 −1
Original line number Diff line number Diff line
@@ -247,16 +247,19 @@ public function testLibrariesExtend() {
  }

  /**
   * Test deprecated libraries.
   * Test library deprecation support.
   *
   * @group legacy
   */
  public function testDeprecatedLibrary() {
    $this->expectDeprecation('Targeting theme_test/moved_from css/foo.css from test_theme_with_deprecated_libraries library_overrides is deprecated in drupal:X.0.0 and will be removed in drupal:Y.0.0. Target theme_test/moved_to css/base-remove.css instead. See https://example.com');
    $this->expectDeprecation('Targeting theme_test/moved_from js/bar.js from test_theme_with_deprecated_libraries library_overrides is deprecated in drupal:X.0.0 and will be removed in drupal:Y.0.0. Target theme_test/moved_to js/foo.js instead. See https://example.com');
    $this->expectDeprecation('Theme "theme_test" is overriding a deprecated library. The "theme_test/deprecated_library" asset library is deprecated in drupal:X.0.0 and is removed from drupal:Y.0.0. Use another library instead. See https://www.example.com');
    $this->expectDeprecation('Theme "theme_test" is extending a deprecated library. The "theme_test/another_deprecated_library" asset library is deprecated in drupal:X.0.0 and is removed from drupal:Y.0.0. Use another library instead. See https://www.example.com');
    $this->expectDeprecation('The "theme_test/deprecated_library" asset library is deprecated in drupal:X.0.0 and is removed from drupal:Y.0.0. Use another library instead. See https://www.example.com');
    $this->expectDeprecation('The "theme_test/another_deprecated_library" asset library is deprecated in drupal:X.0.0 and is removed from drupal:Y.0.0. Use another library instead. See https://www.example.com');
    $this->activateTheme('test_theme_with_deprecated_libraries');
    $this->libraryDiscovery->getLibraryByName('theme_test', 'moved_to');
    $this->libraryDiscovery->getLibraryByName('theme_test', 'deprecated_library');
    $this->libraryDiscovery->getLibraryByName('theme_test', 'another_deprecated_library');
  }