Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pl_attach-library.function.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
$yamlFile = glob('*.libraries.yml');
$yamlOutput = Yaml::parseFile($yamlFile[0]);

$directoryPath = explode('/', dirname(__FILE__));

$rootPathString = '';
// If the themes directory is in the path (Drupal).
if (in_array('themes', $directoryPath)) {
$rootPathLocation = array_search('themes', $directoryPath);
$themePathOffset = -1 * (count($directoryPath) - array_search('components', $directoryPath));
$rootPath = array_slice($directoryPath, $rootPathLocation, $themePathOffset, true);
$rootPathString = implode('/', $rootPath) . '/';
}

// For each item in .libraries.yml file.
foreach($yamlOutput as $key => $value) {

Expand All @@ -22,7 +33,7 @@
$files = $yamlOutput[$key]['js'];
// For each file, create an async script to insert to the Twig component.
foreach($files as $key => $jsPath) {
$scriptString = '<script async src="/' . $key . '"></script>';
$scriptString = '<script async src="/' . $rootPathString . $key . '"></script>';
$stringLoader = \PatternLab\Template::getStringLoader();
$output = $stringLoader->render(array("string" => $scriptString, "data" => []));
return $output;
Expand Down