diff --git a/core/lib/Drupal/Core/Asset/AssetResolver.php b/core/lib/Drupal/Core/Asset/AssetResolver.php index c41e14e..0d679b8 100644 --- a/core/lib/Drupal/Core/Asset/AssetResolver.php +++ b/core/lib/Drupal/Core/Asset/AssetResolver.php @@ -123,23 +123,17 @@ public function getCssAssets(AttachedAssetsInterface $assets, $optimize) { // order. $options['weight'] += count($css) / 1000; - // Add the data to the CSS array depending on the type. + // Depending on the type, some options may need processing. switch ($options['type']) { case 'file': - // Local CSS files are keyed by basename; if a file with the same - // basename is added more than once, it gets overridden. - // By default, take over the filename as basename. + // Local CSS files need the basename to be saved because it is + // used for overriding and removing stylesheets. if (!isset($options['basename'])) { $options['basename'] = drupal_basename($options['data']); } - $css[$options['basename']] = $options; break; - - default: - // External files are keyed by their full URI, so the same CSS - // file is not added twice. - $css[$options['data']] = $options; } + $css[$options['data']] = $options; } } } @@ -151,22 +145,14 @@ public function getCssAssets(AttachedAssetsInterface $assets, $optimize) { // Sort CSS items, so that they appear in the correct order. uasort($css, 'static::sort'); - // Allow themes to remove CSS files by basename. + // Allow themes to remove CSS files by CSS file location. if ($stylesheet_remove = $theme_info->getStyleSheetsRemove()) { foreach ($css as $key => $options) { - if (isset($options['basename']) && isset($stylesheet_remove[$options['basename']])) { + if (isset($stylesheet_remove[$key])) { unset($css[$key]); } } } - // Allow themes to conditionally override CSS files by basename. - if ($stylesheet_override = $theme_info->getStyleSheetsOverride()) { - foreach ($css as $key => $options) { - if (isset($options['basename']) && isset($stylesheet_override[$options['basename']])) { - $css[$key]['data'] = $stylesheet_override[$options['basename']]; - } - } - } if ($optimize) { $css = \Drupal::service('asset.css.collection_optimizer')->optimize($css); diff --git a/core/lib/Drupal/Core/Theme/ActiveTheme.php b/core/lib/Drupal/Core/Theme/ActiveTheme.php index e1d9a96..aace6f9 100644 --- a/core/lib/Drupal/Core/Theme/ActiveTheme.php +++ b/core/lib/Drupal/Core/Theme/ActiveTheme.php @@ -67,13 +67,6 @@ class ActiveTheme { protected $styleSheetsRemove; /** - * The stylesheets which are overridden by the theme. - * - * @var array - */ - protected $styleSheetsOverride; - - /** * The libraries provided by the theme. * * @var array @@ -92,17 +85,16 @@ public function __construct(array $values) { 'engine' => 'twig', 'owner' => 'twig', 'stylesheets_remove' => [], - 'stylesheets_override' => [], 'libraries' => [], 'extension' => 'html.twig', 'base_themes' => [], ]; + $this->name = $values['name']; $this->path = $values['path']; $this->engine = $values['engine']; $this->owner = $values['owner']; $this->styleSheetsRemove = $values['stylesheets_remove']; - $this->styleSheetsOverride = $values['stylesheets_override']; $this->libraries = $values['libraries']; $this->extension = $values['extension']; $this->baseThemes = $values['base_themes']; @@ -165,15 +157,6 @@ public function getLibraries() { } /** - * Returns the overridden stylesheets by the theme. - * - * @return mixed - */ - public function getStyleSheetsOverride() { - return $this->styleSheetsOverride; - } - - /** * Returns the removed stylesheets by the theme. * * @return mixed diff --git a/core/lib/Drupal/Core/Theme/ThemeInitialization.php b/core/lib/Drupal/Core/Theme/ThemeInitialization.php index 29c8205..70bcbf8 100644 --- a/core/lib/Drupal/Core/Theme/ThemeInitialization.php +++ b/core/lib/Drupal/Core/Theme/ThemeInitialization.php @@ -38,6 +38,13 @@ class ThemeInitialization implements ThemeInitializationInterface { protected $root; /** + * The extensions that might be attaching assets. + * + * @var array + */ + protected $extensions; + + /** * Constructs a new ThemeInitialization object. * * @param string $root @@ -51,6 +58,10 @@ public function __construct($root, ThemeHandlerInterface $theme_handler, StateIn $this->root = $root; $this->themeHandler = $theme_handler; $this->state = $state; + + $this->extensions = \Drupal::service('module_handler')->getModuleList(); + $this->extensions = array_merge($this->extensions, $this->themeHandler->listInfo()); + } /** @@ -151,47 +162,26 @@ public function getActiveTheme(Extension $theme, array $base_themes = []) { $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 override parent - // theme stylesheets easily. - // CSS file basenames to override, pointing to the final, overridden filepath. - $values['stylesheets_override'] = array(); - // CSS file basenames to remove. + // We work it this way so that we can have child themes remove CSS files + // easily from parent. $values['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 $basename) { - $values['stylesheets_remove'][$basename] = $base_theme_path . '/' . $basename; - } - } - if (!empty($base->info['stylesheets-override'])) { - foreach ($base->info['stylesheets-override'] as $name) { - $basename = drupal_basename($name); - $values['stylesheets_override'][$basename] = $base_theme_path . '/' . $name; + foreach ($base->info['stylesheets-remove'] as $css_file) { + $css_file = $this->resolveStyleSheetPlaceholders($css_file); + $values['stylesheets_remove'][$css_file] = $css_file; } } } // Add stylesheets used by this theme. if (!empty($theme->info['stylesheets-remove'])) { - foreach ($theme->info['stylesheets-remove'] as $basename) { - $values['stylesheets_remove'][$basename] = $theme_path . '/' . $basename; - - if (isset($values['stylesheets_override'][$basename])) { - unset($values['stylesheets_override'][$basename]); - } - } - } - if (!empty($theme->info['stylesheets-override'])) { - foreach ($theme->info['stylesheets-override'] as $name) { - $basename = drupal_basename($name); - $values['stylesheets_override'][$basename] = $theme_path . '/' . $name; - - if (isset($values['stylesheets_remove'][$basename])) { - unset($values['stylesheets_remove'][$basename]); - } + foreach ($theme->info['stylesheets-remove'] as $css_file) { + $css_file = $this->resolveStyleSheetPlaceholders($css_file); + $values['stylesheets_remove'][$css_file] = $css_file; } } @@ -228,4 +218,24 @@ public function getActiveTheme(Extension $theme, array $base_themes = []) { return new ActiveTheme($values); } + /** + * Resolves tokens from the stylesheets remove. + * + * @param array $list + * + * @return array + * List of stylesheets where placeholders are replaced. + */ + public function resolveStyleSheetPlaceholders($css_file) { + $token_candidate = explode('/', $css_file)[0]; + if (!preg_match('/@[A-z0-9_-]+/', $token_candidate)) { + return $css_file; + } + + $token = substr($token_candidate, 1); + + if (isset($this->extensions[$token])) { + return str_replace($token_candidate, $this->extensions[$token]->getPath(), $css_file); + } + } } diff --git a/core/modules/system/src/Tests/Common/AttachedAssetsTest.php b/core/modules/system/src/Tests/Common/AttachedAssetsTest.php index a52db9b..d111f44 100644 --- a/core/modules/system/src/Tests/Common/AttachedAssetsTest.php +++ b/core/modules/system/src/Tests/Common/AttachedAssetsTest.php @@ -89,7 +89,7 @@ function testAddFiles() { $css = $this->assetResolver->getCssAssets($assets, FALSE); $js = $this->assetResolver->getJsAssets($assets, FALSE)[1]; - $this->assertTrue(array_key_exists('bar.css', $css), 'CSS files are correctly added.'); + $this->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/bar.css', $css), 'CSS files are correctly added.'); $this->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/foo.js', $js), 'JavaScript files are correctly added.'); $css_render_array = \Drupal::service('asset.css.collection_renderer')->render($css); @@ -470,7 +470,7 @@ function testAddJsFileWithQueryString() { $css = $this->assetResolver->getCssAssets($assets, FALSE); $js = $this->assetResolver->getJsAssets($assets, FALSE)[1]; - $this->assertTrue(array_key_exists('querystring.css?arg1=value1&arg2=value2', $css), 'CSS file with query string is correctly added.'); + $this->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/querystring.css?arg1=value1&arg2=value2', $css), 'CSS file with query string is correctly added.'); $this->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2', $js), 'JavaScript file with query string is correctly added.'); $css_render_array = \Drupal::service('asset.css.collection_renderer')->render($css); diff --git a/core/modules/system/src/Tests/Theme/ThemeInfoTest.php b/core/modules/system/src/Tests/Theme/ThemeInfoTest.php index aeb8846..fc42a5b 100644 --- a/core/modules/system/src/Tests/Theme/ThemeInfoTest.php +++ b/core/modules/system/src/Tests/Theme/ThemeInfoTest.php @@ -86,6 +86,10 @@ function testStylesheets() { $this->assertIdentical(0, count($this->xpath("//link[contains(@href, 'sub-remove.css')]")), "sub-remove.css not found"); $this->assertIdentical(0, count($this->xpath("//link[contains(@href, 'base-add.sub-remove.css')]")), "base-add.sub-remove.css not found"); $this->assertIdentical(0, count($this->xpath("//link[contains(@href, 'base-override.sub-remove.css')]")), "base-override.sub-remove.css not found"); + + // Verify that CSS files with the same name are loaded from both the base theme and subtheme. + $this->assertIdentical(1, count($this->xpath("//link[contains(@href, '$base/samename.css')]")), "$base/samename.css found"); + $this->assertIdentical(1, count($this->xpath("//link[contains(@href, '$sub/samename.css')]")), "$sub/samename.css found"); } /** diff --git a/core/modules/system/tests/themes/test_basetheme/.test_basetheme.info.yml.swo b/core/modules/system/tests/themes/test_basetheme/.test_basetheme.info.yml.swo new file mode 100644 index 0000000..c06a858 Binary files /dev/null and b/core/modules/system/tests/themes/test_basetheme/.test_basetheme.info.yml.swo differ 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 1c07b3f..dcf03eb 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 @@ -6,8 +6,8 @@ core: 8.x libraries: - test_basetheme/global-styling stylesheets-override: - - base-override.css - - base-override.sub-remove.css + - @theme_test/css/base-override.css: base-override.css + - @theme_test/css/base-override.sub-remove.css: base-override.sub-remove.css stylesheets-remove: - - base-remove.css - - base-remove.sub-override.css + - @theme_test/css/base-remove.css + - @theme_test/css/base-remove.sub-override.css diff --git a/core/modules/system/tests/themes/test_basetheme/test_basetheme.libraries.yml b/core/modules/system/tests/themes/test_basetheme/test_basetheme.libraries.yml index 1c72d30..49e4ba1 100644 --- a/core/modules/system/tests/themes/test_basetheme/test_basetheme.libraries.yml +++ b/core/modules/system/tests/themes/test_basetheme/test_basetheme.libraries.yml @@ -5,3 +5,4 @@ global-styling: base-add.css: {} base-add.sub-override.css: {} base-add.sub-remove.css: {} + samename.css: {} diff --git a/core/modules/system/tests/themes/test_subtheme/.test_subtheme.info.yml.swp b/core/modules/system/tests/themes/test_subtheme/.test_subtheme.info.yml.swp new file mode 100644 index 0000000..7a3517c Binary files /dev/null and b/core/modules/system/tests/themes/test_subtheme/.test_subtheme.info.yml.swp differ diff --git a/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml b/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml index b15efa9..1d1a19d 100644 --- a/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml +++ b/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml @@ -7,10 +7,10 @@ base theme: test_basetheme libraries: - test_subtheme/global-styling stylesheets-override: - - css/sub-override.css - - css/base-add.sub-override.css - - css/base-remove.sub-override.css + - @theme_test/css/sub-override.css: css/sub-override.css + - @theme_test/css/base-remove.sub-override.css: css/base-remove.sub-override.css + - @test_basetheme/base-add.sub-override.css: css/base-add.sub-override.css stylesheets-remove: - - sub-remove.css - - base-add.sub-remove.css - - base-override.sub-remove.css + - @theme_test/css/sub-remove.css + - @test_basetheme/base-add.sub-remove.css + - @test_basetheme/base-override.sub-remove.css diff --git a/core/modules/system/tests/themes/test_subtheme/test_subtheme.libraries.yml b/core/modules/system/tests/themes/test_subtheme/test_subtheme.libraries.yml index efce7ae..931dffe 100644 --- a/core/modules/system/tests/themes/test_subtheme/test_subtheme.libraries.yml +++ b/core/modules/system/tests/themes/test_subtheme/test_subtheme.libraries.yml @@ -3,3 +3,4 @@ global-styling: css: base: css/sub-add.css: {} + css/samename.css: {} diff --git a/core/themes/seven/.seven.info.yml.swo b/core/themes/seven/.seven.info.yml.swo new file mode 100644 index 0000000..3fc87eb Binary files /dev/null and b/core/themes/seven/.seven.info.yml.swo differ diff --git a/core/themes/seven/seven.theme b/core/themes/seven/seven.theme index c24035e..b4075cb 100644 --- a/core/themes/seven/seven.theme +++ b/core/themes/seven/seven.theme @@ -197,3 +197,6 @@ function seven_form_node_form_alter(&$form, FormStateInterface $form_state) { $form['revision_information']['#type'] = 'container'; $form['revision_information']['#group'] = 'meta'; } + +function seven_library_alter() { +}