diff --git a/core/lib/Drupal/Core/Asset/AssetResolver.php b/core/lib/Drupal/Core/Asset/AssetResolver.php index bc8f69f..b526d88 100644 --- a/core/lib/Drupal/Core/Asset/AssetResolver.php +++ b/core/lib/Drupal/Core/Asset/AssetResolver.php @@ -138,6 +138,7 @@ public function getCssAssets(AttachedAssetsInterface $assets, $optimize) { uasort($css, 'static::sort'); // Allow themes to remove CSS files by CSS files full path and file name. + // @todo Remove in Drupal 9.0.x. if ($stylesheet_remove = $theme_info->getStyleSheetsRemove()) { foreach ($css as $key => $options) { if (isset($stylesheet_remove[$key])) { diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php index 6dd83f7..bcea7f4 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscovery.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscovery.php @@ -9,8 +9,6 @@ use Drupal\Core\Cache\CacheCollectorInterface; use Drupal\Core\Cache\CacheTagsInvalidatorInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Theme\ThemeManagerInterface; /** * Discovers available asset libraries in Drupal. @@ -79,7 +77,25 @@ public function getLibrariesByExtension($extension) { */ public function getLibraryByName($extension, $name) { $extension = $this->getLibrariesByExtension($extension); - return isset($extension[$name]) ? $extension[$name] : FALSE; + if (isset($extension[$name])) { + // Handle libraries that are marked for override or removal. + // @see \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride() + if (isset($extension[$name]['override'])) { + if ($extension[$name]['override']) { + list($new_extension, $new_name) = explode('/', $extension[$name]['override']); + $extension[$name] = $this->getLibraryByName($new_extension, $new_name); + } + else { + unset($extension[$name]); + return FALSE; + } + } + return $extension[$name]; + } + else { + return FALSE; + } + } /** diff --git a/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php index e6a4376..b861ec6 100644 --- a/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php +++ b/core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php @@ -88,6 +88,7 @@ public function buildByExtension($extension) { } $libraries = $this->parseLibraryInfo($extension, $path); + $libraries = $this->applyLibrariesOverride($libraries, $extension); foreach ($libraries as $id => &$library) { if (!isset($library['js']) && !isset($library['css']) && !isset($library['drupalSettings'])) { @@ -185,6 +186,13 @@ public function buildByExtension($extension) { elseif ($this->fileValidUri($source)) { $options['data'] = $source; } + // A regular URI (e.g., http://example.com/example.js) without + // 'external' explicitly specified, which may happen if, e.g. + // libraries-override is used. + elseif ($this->isValidUri($source)) { + $options['type'] = 'external'; + $options['data'] = $source; + } // By default, file paths are relative to the registering extension. else { $options['data'] = $path . '/' . $source; @@ -314,6 +322,72 @@ protected function parseLibraryInfo($extension, $path) { } /** + * Apply libraries overrides specified for the current active theme. + * + * @param array $libraries + * The libraries definitions. + * @param string $extension + * The extension in which these libraries are defined. + * + * @return array + * The modified libraries definitions. + */ + protected function applyLibrariesOverride($libraries, $extension) { + $active_theme = $this->themeManager->getActiveTheme(); + $libraries_overrides = $active_theme->getLibrariesOverride(); + $theme_path = $active_theme->getPath(); + foreach ($libraries as $name => $library) { + // Process libraries overrides. + foreach ($libraries_overrides as $asset => $override) { + // Active theme defines an override for this library. + if ($asset === "$extension/$name") { + // Active theme defines an override for the whole library. Use the + // override key to specify that this library will be overridden when + // it is called. + // @see \Drupal\Core\Asset\LibraryDiscovery::getLibraryByName() + if ($override) { + $libraries[$name]['override'] = $override; + } + else { + $libraries[$name]['override'] = FALSE; + } + } + elseif (strpos($asset, "$extension/$name/") !== FALSE) { + // Active theme defines an override for an asset within this library. + // Throw an exception if the asset is not properly specified. + if (substr_count($asset, '/') < 3) { + throw new \LogicException(sprintf('Library asset %s is not correctly specified. It should be in the form "extension/library_name/sub_key/path/to/asset.js".', $asset)); + } + list(, , $sub_key, $value) = explode('/', $asset, 4); + if ($sub_key === 'drupalSettings') { + // drupalSettings may not be overridden. + throw new \LogicException(sprintf('drupalSettings may not be overridden in libraries-override. Trying to override %s. Use hook_library_info_alter() instead.', $asset)); + } + elseif ($sub_key === 'css') { + // SMACSS category should be incorporated into the asset name. + list($category, $value) = explode('/', $value, 2); + $parents = [$sub_key, $category, $value]; + $new_parents = [$sub_key, $category, $override]; + } + else { + $parents = [$sub_key, $value]; + $new_parents = [$sub_key, $override]; + } + // Remove asset to be overridden, but keep its attributes. + $attributes = NestedArray::getValue($libraries[$name], $parents); + NestedArray::unsetValue($libraries[$name], $parents); + if ($override) { + // Replace with an override if specified. + NestedArray::setValue($libraries[$name], $new_parents, $attributes); + } + } + } + } + + return $libraries; + } + + /** * Wraps drupal_get_path(). */ protected function drupalGetPath($type, $name) { @@ -327,4 +401,11 @@ protected function fileValidUri($source) { return file_valid_uri($source); } + /** + * Determines if the supplied string is a valid URI. + */ + protected function isValidUri($string) { + return count(explode('://', $string)) === 2; + } + } diff --git a/core/lib/Drupal/Core/Theme/ActiveTheme.php b/core/lib/Drupal/Core/Theme/ActiveTheme.php index c35afad..6950c76 100644 --- a/core/lib/Drupal/Core/Theme/ActiveTheme.php +++ b/core/lib/Drupal/Core/Theme/ActiveTheme.php @@ -81,6 +81,13 @@ class ActiveTheme { protected $regions; /** + * The libraries or library assets overridden by the theme. + * + * @var array + */ + protected $librariesOverride; + + /** * Constructs an ActiveTheme object. * * @param array $values @@ -96,6 +103,7 @@ public function __construct(array $values) { 'extension' => 'html.twig', 'base_themes' => [], 'regions' => [], + 'libraries_override' => [], ]; $this->name = $values['name']; @@ -107,6 +115,7 @@ public function __construct(array $values) { $this->extension = $values['extension']; $this->baseThemes = $values['base_themes']; $this->regions = $values['regions']; + $this->librariesOverride = $values['libraries_override']; } /** @@ -169,6 +178,8 @@ public function getLibraries() { * Returns the removed stylesheets by the theme. * * @return mixed + * + * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. */ public function getStyleSheetsRemove() { return $this->styleSheetsRemove; @@ -198,4 +209,25 @@ public function getRegions() { return array_keys($this->regions); } + /** + * Returns the libraries or library assets overridden by the active theme. + * + * The array returned is in the form: + * @code + * [ + * 'core/drupal.collapse' => 'test_theme/collapse', + * 'core/drupal.progress' => FALSE, + * 'classy/base/css/theme/css/layout.css' => 'css/test_theme_layout.css', + * 'core/drupal.dialog/css/theme/misc/dialog.theme.css' => FALSE, + * 'core/jquery/js/assets/vendor/jquery/jquery.min.js' => 'js/collapse.js', + * ] + * @endcode + * + * @return array + * The list of libraries-override. + */ + public function getLibrariesOverride() { + return $this->librariesOverride; + } + } diff --git a/core/lib/Drupal/Core/Theme/ThemeInitialization.php b/core/lib/Drupal/Core/Theme/ThemeInitialization.php index 3f055c1..84f3929 100644 --- a/core/lib/Drupal/Core/Theme/ThemeInitialization.php +++ b/core/lib/Drupal/Core/Theme/ThemeInitialization.php @@ -161,27 +161,27 @@ public function getActiveTheme(Extension $theme, array $base_themes = []) { $values['path'] = $theme_path; $values['name'] = $theme->getName(); - // Prepare stylesheets from this theme as well as all ancestor themes. - // We work it this way so that we can have child themes remove CSS files - // easily from parent. - $values['stylesheets_remove'] = array(); + // @todo Remove in Drupal 9.0.x. + $values['stylesheets_remove'] = $this->prepareStylesheetsRemove($theme, $base_themes); - // Grab stylesheets from base theme. + // Prepare libraries overrides from this theme and ancestor themes. + // This allows child themes to easily remove CSS files from base themes and + // modules. + $values['libraries_override'] = []; + + // Get libraries-override declared by base theme. foreach ($base_themes as $base) { - $base_theme_path = $base->getPath(); - if (!empty($base->info['stylesheets-remove'])) { - foreach ($base->info['stylesheets-remove'] as $css_file) { - $css_file = $this->resolveStyleSheetPlaceholders($css_file); - $values['stylesheets_remove'][$css_file] = $css_file; + if (!empty($base->info['libraries-override'])) { + foreach ($base->info['libraries-override'] as $library => $override) { + $values['libraries_override'][$library] = $override ? $this->resolveThemeAssetPath($base, $library, $override) : $override; } } } - // Add stylesheets used by this theme. - if (!empty($theme->info['stylesheets-remove'])) { - foreach ($theme->info['stylesheets-remove'] as $css_file) { - $css_file = $this->resolveStyleSheetPlaceholders($css_file); - $values['stylesheets_remove'][$css_file] = $css_file; + // Add libraries-override declared by this theme. + if (!empty($theme->info['libraries-override'])) { + foreach ($theme->info['libraries-override'] as $library => $override) { + $values['libraries_override'][$library] = $override ? $this->resolveThemeAssetPath($theme, $library, $override) : $override; } } @@ -241,6 +241,8 @@ protected function getExtensions() { * * @return string * CSS file where placeholders are replaced. + * + * @todo Remove in Drupal 9.0.x. */ protected function resolveStyleSheetPlaceholders($css_file) { $token_candidate = explode('/', $css_file)[0]; @@ -256,4 +258,82 @@ protected function resolveStyleSheetPlaceholders($css_file) { return str_replace($token_candidate, $extensions[$token]->getPath(), $css_file); } } + + /** + * Prepares stylesheets-remove specified in the *.info.yml file. + * + * @param \Drupal\Core\Extension\Extension $theme + * The theme extension object. + * @param \Drupal\Core\Extension\Extension[] $base_themes + * An array of base themes. + * + * @return string[] + * The list of stylesheets-remove specified in the *.info.yml file. + * + * @todo Remove in Drupal 9.0.x. + */ + protected function prepareStylesheetsRemove(Extension $theme, $base_themes) { + // Prepare stylesheets from this theme as well as all ancestor themes. + // We work it this way so that we can have child themes remove CSS files + // easily from parent. + $stylesheets_remove = array(); + // Grab stylesheets from base theme. + foreach ($base_themes as $base) { + $base_theme_path = $base->getPath(); + if (!empty($base->info['stylesheets-remove'])) { + foreach ($base->info['stylesheets-remove'] as $css_file) { + $css_file = $this->resolveStyleSheetPlaceholders($css_file); + $stylesheets_remove[$css_file] = $css_file; + } + } + } + + // Add stylesheets used by this theme. + if (!empty($theme->info['stylesheets-remove'])) { + foreach ($theme->info['stylesheets-remove'] as $css_file) { + $css_file = $this->resolveStyleSheetPlaceholders($css_file); + $stylesheets_remove[$css_file] = $css_file; + } + } + return $stylesheets_remove; + } + + /** + * Ensures that a full path is returned for an overriding theme asset. + * + * If the specified $source is a library, then the $destination is returned + * without change. + * + * @param \Drupal\Core\Extension\Extension $theme + * The theme or base theme. + * @param string $source + * The source asset path, i.e. the library to be replaced. + * @param string $destination + * The library or library asset replacing the source. + * + * @return string + * A fully resolved theme asset path relative to the Drupal directory. + */ + protected function resolveThemeAssetPath(Extension $theme, $source, $destination) { + // Full library definitions don't need to be resolved. + if (count(explode('/', $source)) === 2) { + return $destination; + } + // The destination is not an absolute path and it's not a URI (e.g. + // public://generated_js/example.js or http://example.com/js/my_js.js), so + // it's relative to the theme. + if ($destination[0] !== '/' && !$this->isValidUri($destination)) { + $theme_path = $theme->getPath(); + return '/' . $theme_path . '/' . $destination; + } + return $destination; + } + + /** + * Determines if the supplied string is a valid URI. + */ + protected function isValidUri($string) { + return count(explode('://', $string)) === 2; + } + } diff --git a/core/modules/system/src/Tests/Asset/LibraryDiscoveryIntegrationTest.php b/core/modules/system/src/Tests/Asset/LibraryDiscoveryIntegrationTest.php index 2ddccea..e29508c 100644 --- a/core/modules/system/src/Tests/Asset/LibraryDiscoveryIntegrationTest.php +++ b/core/modules/system/src/Tests/Asset/LibraryDiscoveryIntegrationTest.php @@ -42,4 +42,213 @@ public function testElementInfoByTheme() { $this->assertTrue($library_discovery->getLibraryByName('test_theme', 'kitten')); } + /** + * Tests that libraries-override are applied to library definitions. + */ + public function testLibrariesOverride() { + /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */ + $theme_initializer = $this->container->get('theme.initialization'); + + /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */ + $theme_manager = $this->container->get('theme.manager'); + + /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */ + $library_discovery = $this->container->get('library.discovery'); + + $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('test_theme')); + + // Assert that entire library was correctly overridden. + $this->assertEqual($library_discovery->getLibraryByName('core', 'drupal.collapse'), $library_discovery->getLibraryByName('test_theme', 'collapse'), 'Entire library correctly overridden.'); + + // Assert that library asset was correctly overridden. + $library = $library_discovery->getLibraryByName('classy', 'base'); + $this->assertAssetInLibrary($library['css'], 'base', 'core/modules/system/tests/themes/test_theme/css/test_theme_layout.css'); + + // Assert that entire library was correctly removed. + $this->assertFalse($library_discovery->getLibraryByName('core', 'drupal.progress'), 'Entire library correctly removed.'); + + // Assert that library asset was correctly removed. + $library = $library_discovery->getLibraryByName('core', 'drupal.dialog'); + $this->assertNoAssetInLibrary($library['css'], 'drupal.dialog', 'core/misc/dialog.theme.css'); + + // Assert that overridden library asset still retains attributes. + $library = $library_discovery->getLibraryByName('core', 'jquery'); + foreach ($library['js'] as $definition) { + if ($definition['data'] == 'core/modules/system/tests/themes/test_theme/js/collapse.js') { + $this->assertTrue($definition['minified'] && $definition['weight'] == -20, 'Previous attributes retained'); + break; + } + } + } + + /** + * Tests libraries-override on drupalSettings. + */ + public function testLibrariesOverrideDrupalSettings() { + $this->container->get('theme_handler')->install(['test_theme_libraries_override_with_drupal_settings']); + + /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */ + $theme_initializer = $this->container->get('theme.initialization'); + + /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */ + $theme_manager = $this->container->get('theme.manager'); + + /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */ + $library_discovery = $this->container->get('library.discovery'); + + $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('test_theme_libraries_override_with_drupal_settings')); + + // Assert that drupalSettings cannot be overridden and throws an exception. + try { + $library_discovery->getLibraryByName('core', 'drupal.ajax'); + $this->fail('Throw LogicException when trying to override drupalSettings'); + } + catch (\LogicException $e) { + $expected_message = 'drupalSettings may not be overridden in libraries-override. Trying to override core/drupal.ajax/drupalSettings/ajaxPageState. Use hook_library_info_alter() instead.'; + $this->assertEqual($e->getMessage(), $expected_message, 'Throw LogicException when trying to override drupalSettings'); + } + } + + /** + * Tests libraries-override on malformed assets. + */ + public function testLibrariesOverrideMalformedAsset() { + $this->container->get('theme_handler')->install(['test_theme_libraries_override_with_invalid_asset']); + + /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */ + $theme_initializer = $this->container->get('theme.initialization'); + + /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */ + $theme_manager = $this->container->get('theme.manager'); + + /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */ + $library_discovery = $this->container->get('library.discovery'); + + $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('test_theme_libraries_override_with_invalid_asset')); + + // Assert that improperly formed asset "specs" throw an exception. + try { + $library_discovery->getLibraryByName('core', 'drupal.dialog'); + $this->fail('Throw LogicException when specifying invalid override'); + } + catch (\LogicException $e) { + $expected_message = 'Library asset core/drupal.dialog/css is not correctly specified. It should be in the form "extension/library_name/sub_key/path/to/asset.js".'; + $this->assertEqual($e->getMessage(), $expected_message, 'Throw LogicException when specifying invalid override'); + } + } + + /** + * Tests library assets with other ways for specifying paths. + */ + public function testLibrariesOverrideOtherAssetLibraryNames() { + $this->container->get('theme_handler')->install(['test_theme']); + + /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */ + $theme_initializer = $this->container->get('theme.initialization'); + + /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */ + $theme_manager = $this->container->get('theme.manager'); + + /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */ + $library_discovery = $this->container->get('library.discovery'); + + $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('test_theme')); + + // Assert Drupal relative paths. + $library = $library_discovery->getLibraryByName('core', 'drupal.dropbutton'); + $this->assertAssetInLibrary($library['css'], 'drupal.dropbutton', 'themes/my_theme/css/dropbutton.css'); + + // Assert streamwrapper paths. + $library = $library_discovery->getLibraryByName('core', 'drupal.vertical-tabs'); + $this->assertAssetInLibrary($library['css'], 'drupal.vertical-tabs', 'public://my_css/vertical-tabs.css'); + + // Assert protocol-free URI. + $library = $library_discovery->getLibraryByName('core', 'jquery.ui'); + $this->assertAssetInLibrary($library['css'], 'jquery.ui', '//my-server/my_theme/css/jquery_ui.css'); + + // Assert regular URI. + $library = $library_discovery->getLibraryByName('core', 'jquery.farbtastic'); + $this->assertAssetInLibrary($library['css'], 'jquery.farbtastic', 'http://example.com/my_theme/css/farbtastic.css'); + } + + /** + * Tests that base theme libraries-remove still apply in sub themes. + */ + public function testBaseThemeLibrariesOverrideInSubTheme() { + $this->container->get('theme_handler')->install(['test_subtheme']); + + /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */ + $theme_initializer = $this->container->get('theme.initialization'); + + /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */ + $theme_manager = $this->container->get('theme.manager'); + + /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */ + $library_discovery = $this->container->get('library.discovery'); + + $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('test_subtheme')); + + // Assert that libraries-override specified in the base theme still applies + // in sub theme. + $library = $library_discovery->getLibraryByName('core', 'drupal.dialog'); + $this->assertNoAssetInLibrary($library['js'], 'drupal.dialog', 'core/misc/dialog/dialog.js'); + + $library = $library_discovery->getLibraryByName('core', 'jquery.farbtastic'); + $this->assertAssetInLibrary($library['css'], 'jquery.farbtastic', 'core/modules/system/tests/themes/test_basetheme/css/farbtastic.css'); + } + + /** + * Asserts that the given asset is in the specified library. + * + * @param mixed $library_item + * The library where the given asset should be found. + * @param string $library_name + * Name of the library. + * @param string $asset + * The asset file with the path for the file. + * @param string $message + * (optional) A message to display with the assertion. + * + * @return bool + * TRUE if the specified asset is found in the library. + */ + protected function assertAssetInLibrary($library_item, $library_name, $asset, $message = NULL) { + if (!isset($message)) { + $message = sprintf('Asset %s found in library %s', $asset, $library_name); + } + foreach ($library_item as $definition) { + if ($asset == $definition['data']) { + return $this->pass($message); + } + } + return $this->fail($message); + } + + /** + * Asserts that the given asset is not in the specified library. + * + * @param mixed $library_item + * The library where the given asset should not be found. + * @param string $library_name + * Name of the library. + * @param string $asset + * The asset file with the path for the file. + * @param string $message + * (optional) A message to display with the assertion. + * + * @return bool + * TRUE if the specified asset is not found in the library. + */ + protected function assertNoAssetInLibrary($library_item, $library_name, $asset, $message = NULL) { + if (!isset($message)) { + $message = sprintf('Asset %s not found in library %s', $asset, $library_name); + } + foreach ($library_item as $definition) { + if ($asset == $definition['data']) { + return $this->fail($message); + } + } + return $this->pass($message); + } + } diff --git a/core/modules/system/src/Tests/Theme/ThemeTest.php b/core/modules/system/src/Tests/Theme/ThemeTest.php index e62a199..ffb2998 100644 --- a/core/modules/system/src/Tests/Theme/ThemeTest.php +++ b/core/modules/system/src/Tests/Theme/ThemeTest.php @@ -167,7 +167,7 @@ function testCSSOverride() { $config->set('css.preprocess', 0); $config->save(); $this->drupalGet('theme-test/suggestion'); - $this->assertNoText('system.module.css', 'The theme\'s .info.yml file is able to override a module CSS file from being added to the page.'); + $this->assertNoText('system.module.css', "The theme's .info.yml file is able to remove a module CSS file from being added to the page."); // Also test with aggregation enabled, simply ensuring no PHP errors are // triggered during drupal_build_css_cache() when a source file doesn't diff --git a/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml b/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml index dcb1a2f..587070c 100644 --- a/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml +++ b/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml @@ -7,3 +7,6 @@ libraries: - test_basetheme/global-styling stylesheets-remove: - '@theme_test/css/base-remove.css' +libraries-override: + core/drupal.dialog/js/misc/dialog/dialog.js: false + core/jquery.farbtastic/css/component/assets/vendor/farbtastic/farbtastic.css: css/farbtastic.css diff --git a/core/modules/system/tests/themes/test_theme/css/collapse.css b/core/modules/system/tests/themes/test_theme/css/collapse.css new file mode 100644 index 0000000..23f38b3 --- /dev/null +++ b/core/modules/system/tests/themes/test_theme/css/collapse.css @@ -0,0 +1,4 @@ +/** + * @file + * Test CSS asset file for test_theme.theme. + */ diff --git a/core/modules/system/tests/themes/test_theme/js/collapse.js b/core/modules/system/tests/themes/test_theme/js/collapse.js new file mode 100644 index 0000000..4d66841 --- /dev/null +++ b/core/modules/system/tests/themes/test_theme/js/collapse.js @@ -0,0 +1,4 @@ +/** + * @file + * Test JS asset file for test_theme.theme. + */ diff --git a/core/modules/system/tests/themes/test_theme/test_theme.info.yml b/core/modules/system/tests/themes/test_theme/test_theme.info.yml index e9d6602..40a52a9 100644 --- a/core/modules/system/tests/themes/test_theme/test_theme.info.yml +++ b/core/modules/system/tests/themes/test_theme/test_theme.info.yml @@ -18,6 +18,25 @@ stylesheets-remove: - '@system/css/system.module.css' libraries: - test_theme/global-styling +libraries-override: + # Replace an entire library. + core/drupal.collapse: test_theme/collapse + # Remove an entire library. + core/drupal.progress: false + # Replace one particular library asset with another. + classy/base/css/theme/css/layout.css: css/test_theme_layout.css + # Remove one particular asset. + core/drupal.dialog/css/theme/misc/dialog.theme.css: false + # It works for JS as well. + core/jquery/js/assets/vendor/jquery/jquery.min.js: js/collapse.js + # Use Drupal relative paths. + core/drupal.dropbutton/css/component/misc/dropbutton/dropbutton.css: /themes/my_theme/css/dropbutton.css + # Use streamwrappers. + core/drupal.vertical-tabs/css/component/misc/vertical-tabs.css: public://my_css/vertical-tabs.css + # Use protocol-free URI. + core/jquery.ui/css/component/assets/vendor/jquery.ui/themes/base/core.css: //my-server/my_theme/css/jquery_ui.css + # Use regular URI. + core/jquery.farbtastic/css/component/assets/vendor/farbtastic/farbtastic.css: http://example.com/my_theme/css/farbtastic.css regions: content: Content left: Left diff --git a/core/modules/system/tests/themes/test_theme/test_theme.libraries.yml b/core/modules/system/tests/themes/test_theme/test_theme.libraries.yml index c1fe4a5..d82f1d8 100644 --- a/core/modules/system/tests/themes/test_theme/test_theme.libraries.yml +++ b/core/modules/system/tests/themes/test_theme/test_theme.libraries.yml @@ -3,3 +3,10 @@ global-styling: css: base: kitten.css: {} +collapse: + js: + js/collapse.js: { } + + css: + base: + css/collapse.css: { } diff --git a/core/modules/system/tests/themes/test_theme_libraries_override_with_drupal_settings/test_theme_libraries_override_with_drupal_settings.info.yml b/core/modules/system/tests/themes/test_theme_libraries_override_with_drupal_settings/test_theme_libraries_override_with_drupal_settings.info.yml new file mode 100644 index 0000000..0939c27 --- /dev/null +++ b/core/modules/system/tests/themes/test_theme_libraries_override_with_drupal_settings/test_theme_libraries_override_with_drupal_settings.info.yml @@ -0,0 +1,9 @@ +name: 'Test theme libraries-override' +type: theme +description: 'Theme with drupalSettings libraries-override' +version: VERSION +base theme: classy +core: 8.x +libraries-override: + # drupalSettings libraries override. Should throw a \LogicException. + core/drupal.ajax/drupalSettings/ajaxPageState: { } diff --git a/core/modules/system/tests/themes/test_theme_libraries_override_with_invalid_asset/test_theme_libraries_override_with_invalid_asset.info.yml b/core/modules/system/tests/themes/test_theme_libraries_override_with_invalid_asset/test_theme_libraries_override_with_invalid_asset.info.yml new file mode 100644 index 0000000..ecba74e --- /dev/null +++ b/core/modules/system/tests/themes/test_theme_libraries_override_with_invalid_asset/test_theme_libraries_override_with_invalid_asset.info.yml @@ -0,0 +1,9 @@ +name: 'Test theme libraries-override' +type: theme +description: 'Theme with invalid libraries-override asset spec.' +version: VERSION +base theme: classy +core: 8.x +libraries-override: + # A malformed library asset name. Should throw a \LogicException. + core/drupal.dialog/css: false diff --git a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php index 9865cb2..2f5ed21 100644 --- a/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryParserTest.php @@ -73,6 +73,15 @@ protected function setUp() { $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); $this->themeManager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface'); + $mock_active_theme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme') + ->disableOriginalConstructor() + ->getMock(); + $mock_active_theme->expects($this->any()) + ->method('getLibrariesOverride') + ->willReturn([]); + $this->themeManager->expects($this->any()) + ->method('getActiveTheme') + ->willReturn($mock_active_theme); $this->libraryDiscoveryParser = new TestLibraryDiscoveryParser($this->root, $this->moduleHandler, $this->themeManager); } diff --git a/core/tests/Drupal/Tests/Core/Theme/RegistryTest.php b/core/tests/Drupal/Tests/Core/Theme/RegistryTest.php index 4a9101d..71a71f4 100644 --- a/core/tests/Drupal/Tests/Core/Theme/RegistryTest.php +++ b/core/tests/Drupal/Tests/Core/Theme/RegistryTest.php @@ -93,7 +93,7 @@ public function testGetRegistryForModule() { 'engine' => 'twig', 'owner' => 'twig', 'stylesheets_remove' => [], - 'stylesheets_override' => [], + 'libraries_override' => [], 'libraries' => [], 'extension' => '.twig', 'base_themes' => [], diff --git a/core/themes/bartik/bartik.info.yml b/core/themes/bartik/bartik.info.yml index fb8f2fb..fa73bcb 100644 --- a/core/themes/bartik/bartik.info.yml +++ b/core/themes/bartik/bartik.info.yml @@ -5,10 +5,10 @@ description: 'A flexible, recolorable theme with many regions and a responsive, package: Core version: VERSION core: 8.x -stylesheets-remove: - - '@classy/css/layout.css' libraries: - bartik/global-styling +libraries-override: + classy/base/css/theme/css/layout.css: false ckeditor_stylesheets: - css/base/elements.css - css/components/captions.css @@ -35,4 +35,3 @@ regions: footer_third: 'Footer third' footer_fourth: 'Footer fourth' footer_fifth: 'Footer fifth' - diff --git a/core/themes/seven/seven.info.yml b/core/themes/seven/seven.info.yml index 99a3ec1..daf74a0 100644 --- a/core/themes/seven/seven.info.yml +++ b/core/themes/seven/seven.info.yml @@ -8,9 +8,9 @@ version: VERSION core: 8.x libraries: - seven/global-styling -stylesheets-remove: - - core/assets/vendor/jquery.ui/themes/base/dialog.css - - '@classy/css/layout.css' +libraries-override: + core/jquery.ui.dialog/css/component/assets/vendor/jquery.ui/themes/base/dialog.css: false + classy/base/css/theme/css/layout.css: false quickedit_stylesheets: - css/components/quickedit.css regions: