Verified Commit 575c5331 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3357382 by Robert Ngo, e0ipso, smustgrave: Unable to override library...

Issue #3357382 by Robert Ngo, e0ipso, smustgrave: Unable to override library auto-definition to add external CSS & JS
parent 9c505099
Loading
Loading
Loading
Loading
+17 −6
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Component\Assertion\Inspector;
use Drupal\Component\Discovery\YamlDirectoryDiscovery;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
@@ -385,16 +386,26 @@ private function translateLibraryPaths(array $overrides, string $component_direc
    $js = $overrides['js'] ?? [];
    foreach ($css as $dir => $css_info) {
      foreach ($css_info as $filename => $options) {
        if (!UrlHelper::isExternal($filename)) {
          $absolute_filename = sprintf('%s%s%s', $component_directory, DIRECTORY_SEPARATOR, $filename);
          $altered_filename = $this->makePathRelativeToLibraryRoot($absolute_filename);
          $altered_overrides['css'][$dir][$altered_filename] = $options;
        }
        else {
          $altered_overrides['css'][$dir][$filename] = $options;
        }
      }
    }
    foreach ($js as $filename => $options) {
      if (!UrlHelper::isExternal($filename)) {
        $absolute_filename = sprintf('%s%s%s', $component_directory, DIRECTORY_SEPARATOR, $filename);
        $altered_filename = $this->makePathRelativeToLibraryRoot($absolute_filename);
        $altered_overrides['js'][$altered_filename] = $options;
      }
      else {
        $altered_overrides['js'][$filename] = $options;
      }
    }
    return $altered_overrides;
  }

+4 −0
Original line number Diff line number Diff line
@@ -42,6 +42,10 @@ public function testLibraryOverrides(): void {
    $this->assertStringContainsString('dialog.position.js', $output);
    // Ensure that libraryOverrides processes attributes properly.
    $this->assertMatchesRegularExpression('@<script.*src="[^"]*lib-overrides\.js\?v=1[^"]*".*defer.*bar="foo"></script>@', $output);
    // Ensure that libraryOverrides processes external CSS properly.
    $this->assertMatchesRegularExpression('@<link.*href="https://drupal\.org/fake-dependency/styles\.css" />@', $output);
    // Ensure that libraryOverrides processes external JS properly.
    $this->assertMatchesRegularExpression('@<script.*src="https://drupal\.org/fake-dependency/index\.min\.js"></script>@', $output);
  }

}
+3 −1
Original line number Diff line number Diff line
@@ -8,5 +8,7 @@ libraryOverrides:
  css:
    component:
      another-stylesheet.css: {}
      https://drupal.org/fake-dependency/styles.css: {}
  js:
    lib-overrides.js: { attributes: { defer: true, bar: foo } }
    https://drupal.org/fake-dependency/index.min.js: {}