Problem/Motivation
When a component declare a library, its path is made relative to core because https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...
/**
* Takes a path and makes it relative to the library provider.
*
* Drupal will take a path relative to the library provider in order to put
* CSS and JS in the HTML page. Core is the provider for all the
* auto-generated libraries for the components. This means that in order to
* add <root>/themes/custom/my_theme/components/my-component/my-component.css
* in the page, we need to crawl back up from <root>/core first:
* ../themes/custom/my_theme/components/my-component/my-component.css.
*
* @param string $path
* The path to the file.
*
* @return string
* The path relative to the library provider root.
*/
private function makePathRelativeToLibraryRoot(string $path): string {
$path_from_root = str_starts_with($path, $this->appRoot)
? substr($path, strlen($this->appRoot) + 1)
: $path;
// The library owner is in <root>/core, so we need to go one level up to
// find the app root.
return '../' . $path_from_root;
}
Currently, it is not possible to a theme to alter a component library with its .info.yml file.
For example, the following code will not work (.info.yml of the theme):
libraries-override:
core/components.my_theme--navbar:
css:
component:
styles/navbar.css: false or replace
Because the path to the file when debugging library alteration is:
../path/to/my_theme/components/navbar/styles/navbar.css
So the CSS is not removed/replaced. It should be:
libraries-override:
core/components.my_theme--navbar:
css:
component:
../path/to/my_theme/components/navbar/styles/navbar.css: false or replace
Which can't be relied on when preparing if the theme will be gathered as a Composer package and so placed potentially in a contrib folder and not a custom folder like on the env used to develop it.
Steps to reproduce
Proposed resolution
Either:
- the provider should be the module/theme declaring the component
- the path should not depend on the path of the Drupal extension
- in the library override processing, when searching for matches, concat with the extension path automatically?