function ComponentPluginManager::makePathRelativeToLibraryRoot

Same name in this branch
  1. 11.x core/lib/Drupal/Core/Theme/ComponentPluginManager.php \Drupal\Core\Theme\ComponentPluginManager::makePathRelativeToLibraryRoot()
Same name and namespace in other branches
  1. 10 core/modules/sdc/src/ComponentPluginManager.php \Drupal\sdc\ComponentPluginManager::makePathRelativeToLibraryRoot()
  2. 10 core/lib/Drupal/Core/Theme/ComponentPluginManager.php \Drupal\Core\Theme\ComponentPluginManager::makePathRelativeToLibraryRoot()

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. The SDC module 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/modules/sdc first: ../../../../themes/custom/my_theme/components/my-component/my-component.css

Parameters

string $path: The path to the file.

Return value

string The path relative to the library provider root.

2 calls to ComponentPluginManager::makePathRelativeToLibraryRoot()
ComponentPluginManager::findAsset in core/modules/sdc/src/ComponentPluginManager.php
Finds assets related to the provided metadata file.
ComponentPluginManager::translateLibraryPaths in core/modules/sdc/src/ComponentPluginManager.php
Changes the library paths, so they can be used by the library system.

File

core/modules/sdc/src/ComponentPluginManager.php, line 475

Class

ComponentPluginManager
Defines a plugin manager to deal with sdc.

Namespace

Drupal\sdc

Code

private function makePathRelativeToLibraryRoot(string $path) : string {
    $library_provider_root = $this->moduleHandler
        ->getModule('sdc')
        ->getPath();
    $num_dots = count(array_filter(explode(DIRECTORY_SEPARATOR, $library_provider_root)));
    $dots = str_repeat('../', $num_dots);
    $path_from_root = str_starts_with($path, $this->appRoot) ? substr($path, strlen($this->appRoot) + 1) : $path;
    return $dots . $path_from_root;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.