Add site method to get a list of asset filenames via a hook.

This commit is contained in:
2026-04-17 02:07:30 +10:00
parent ba8bb3cbae
commit 84f1eda2da

View File

@@ -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" => [[