diff --git a/pl_attach-library.function.php b/pl_attach-library.function.php index 00ed98e..ca79388 100644 --- a/pl_attach-library.function.php +++ b/pl_attach-library.function.php @@ -1,10 +1,12 @@ $value) { + foreach ($yamlOutput as $key => $value) { // If the library exists. if ($key === $libraryName) { - $files = $yamlOutput[$key]['js']; - // For each file, create an async script to insert to the Twig component. - foreach($files as $key => $file) { - // By default prefix paths with a /, but remove this for external JS - // as it would break URLs. - $path_prefix = '/'; - if (isset($file['type']) && $file['type'] === 'external') { - $path_prefix = ''; + if (isset($yamlOutput[$key]['js'])) { + $js_files = $yamlOutput[$key]['js']; + } + // Check if CSS files are defined. + if (isset($yamlOutput[$key]['css'])) { + // Create a single array from stylesheets groups (base, theme). + $css_files = array_reduce($yamlOutput[$key]['css'], 'array_merge', []); + } + + // For each JS file, create an async script to insert to the Twig component. + if (isset($js_files)) { + foreach ($js_files as $key => $file) { + // By default prefix paths with relative path to dist folder, + // but remove this for external JS as it would break URLs. + $path_prefix = '/'; + if (isset($file['type']) && $file['type'] === 'external') { + $path_prefix = ''; + } + $scriptString = ''; + $stringLoader = Template::getStringLoader(); + $scriptTags[$key] = $stringLoader->render(["string" => $scriptString, "data" => []]); } - $scriptString = ''; - $stringLoader = \PatternLab\Template::getStringLoader(); - $scriptTags[$key] = $stringLoader->render(array("string" => $scriptString, "data" => [])); - } + } + + // For each CSS file, create a stylesheet link to insert to the Twig component. + if (isset($css_files)) { + foreach ($css_files as $key => $file) { + // By default prefix paths with relative path to dist folder, + // but remove this for external CSS as it would break URLs. + $path_prefix = '/'; + if (isset($file['type']) && $file['type'] === 'external') { + $path_prefix = ''; + } + $linkString = ''; + $stringLoader = Template::getStringLoader(); + $styleTags[$key] = $stringLoader->render(["string" => $linkString, "data" => []]); + } + } } } - - return implode($scriptTags); -}, array('is_safe' => array('html'))); + echo implode($styleTags); + echo implode($scriptTags); +}, ['is_safe' => ['html']]);