From 959dfc1ca55479e788b3c6c675bea9c1a02a2332 Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Mon, 18 Feb 2019 11:14:12 +0100 Subject: [PATCH 1/2] Fix PHPCS errors Add missing spaces. Keep array syntax consistent and short. --- pl_attach-library.function.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pl_attach-library.function.php b/pl_attach-library.function.php index 00ed98e..b6a51b8 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) { + foreach ($files as $key => $file) { // By default prefix paths with a /, but remove this for external JS // as it would break URLs. $path_prefix = '/'; @@ -30,11 +32,11 @@ $path_prefix = ''; } $scriptString = ''; - $stringLoader = \PatternLab\Template::getStringLoader(); - $scriptTags[$key] = $stringLoader->render(array("string" => $scriptString, "data" => [])); - } + $stringLoader = Template::getStringLoader(); + $scriptTags[$key] = $stringLoader->render(["string" => $scriptString, "data" => []]); + } } } return implode($scriptTags); -}, array('is_safe' => array('html'))); +}, ['is_safe' => ['html']]); From 611c5ea26aa637be6dac62df04dfe86092df344e Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Mon, 18 Feb 2019 11:32:36 +0100 Subject: [PATCH 2/2] Add ability to attach styles and/or scripts If CSS files, create merged array of files for each group (base, theme). Then for each CSS file create link stylesheet. Wrap foreach js files in if statement so library can contain JS or CSS. --- pl_attach-library.function.php | 52 +++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/pl_attach-library.function.php b/pl_attach-library.function.php index b6a51b8..ca79388 100644 --- a/pl_attach-library.function.php +++ b/pl_attach-library.function.php @@ -16,27 +16,53 @@ $yamlFile = glob('*.libraries.yml'); $yamlOutput = Yaml::parseFile($yamlFile[0]); $scriptTags = []; + $styleTags = []; // For each item in .libraries.yml file. 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" => []]); + } + } + + // 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" => []]); } - $scriptString = ''; - $stringLoader = Template::getStringLoader(); - $scriptTags[$key] = $stringLoader->render(["string" => $scriptString, "data" => []]); } } } - - return implode($scriptTags); + echo implode($styleTags); + echo implode($scriptTags); }, ['is_safe' => ['html']]);