From 84f1eda2da5f61c248e900c01344c66c493d8e4d Mon Sep 17 00:00:00 2001 From: Dreytac Date: Fri, 17 Apr 2026 02:07:30 +1000 Subject: [PATCH] Add site method to get a list of asset filenames via a hook. --- index.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/index.php b/index.php index 6727cae..7dc2dfe 100644 --- a/index.php +++ b/index.php @@ -14,6 +14,55 @@ Kirby::plugin( name: "hobbyhome/library", extends: [ + "options" => [ + "assets" => [ + "css" => [ + # 100 => Url::path(kirby()->url("media") . "/plugins/hobbyhome/library/css/example.css", true), + ], + ], + ], + "hooks" => [ + /** + * To add this hook to a plugin, change the option() call to use the plugins name. + * + * @version 1.0 + */ + "hobbyhome.getAssets" => function ($files = [], $type = "css") { + foreach (option("hobbyhome.library.assets.{$type}", []) as $order => $file) { + # Make sure no other file exists with the current sort order index. + while (isset($files[$order])) { + $order++; + } + + $files[$order] = $file; + } + + return $files; + }, + ], + "siteMethods" => [ + /** + * Fetch and sort a list of asset filenames. + * + * @param string $type The type of asset filenames to fetch. + * + * @return Array of asset filenames, sorted by index. + */ + "getAssets" => function ($type = "css") { + $hookFiles = kirby()->apply("hobbyhome.getAssets", ["files" => [], "type" => $type], "files"); + $files = []; + + foreach ($hookFiles as $order => $file) { + # If file exists, add MD5 query string to attempt to cache bust it. + $md5 = F::exists(kirby()->root() . $file) ? md5_file(kirby()->root() . $file) : ""; + $files[$order] = url($file, ["query" => ["md5" => $md5]]); + } + + ksort($files, SORT_NUMERIC); + + return $files; + }, + ], ], info: [ "authors" => [[