Compare commits

..

2 Commits

4 changed files with 204 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
type: fields
fields:
linkLocation:
label: Link Location
type: select
options:
type: query
query: site.linkLocations.toStructure
text: "{{ item.description }}"
value: "{{ item.location }}"
linkIndex:
label: Link Position
type: number
min: 1
default: 10
linkText:
label: Link Text
help: Custom link text to use instead of the title.
type: text
linkIcon:
label: Link Icon
help: Font Awesome icon that is used for the link text.
type: text

View File

@@ -0,0 +1,15 @@
type: fields
fields:
linkLocations:
label: Link Locations
type: structure
fields:
description:
label: Location Description
type: text
help: A friendly description for selecting this location on pages.
location:
label: Location Key
type: text
help: This is what is used when finding links for this location.

View File

@@ -21,6 +21,13 @@ Kirby::plugin(
], ],
], ],
], ],
"blueprints" => [
"fields/link-locations" => __DIR__ . "/blueprints/fields/link-locations.yml",
"fields/link-location" => __DIR__ . "/blueprints/fields/link-location.yml",
],
"snippets" => [
"navbar/primary" => __DIR__ . "/snippets/navbar/primary.php",
],
"hooks" => [ "hooks" => [
/** /**
* To add this hook to a plugin, change the option() call to use the plugins name. * To add this hook to a plugin, change the option() call to use the plugins name.
@@ -39,6 +46,74 @@ Kirby::plugin(
return $files; return $files;
}, },
/**
* To add this hook to a plugin, replace the foreach filter with any other method to generate the $newLinks[] array.
*
* @version 1.0
*
* Links in the array should have a minimum of the following options set:
* $newLinks[] => [
* "index" => The sort index of the link,
* "url" => The URL of the link,
* "text" => The text of the link,
* "aria" => Accessible version of the link text,
* "active" => True if this is the active link,
* ];
*
* Optional supported options are:
* $newLinks[] = [
* "icon" => An optional Font Awesome icon,
* "disabled" => True if this link should have the 'disabled' HTML attribute,
* "target" => The value of the HTML link target attribute,
* "dropdown" => [
* [
* "divider" => True to display a divider in the dropdown menu,
* ],
* [
* A link, as defined above, that should be displayed in the dropdown menu.
* These links will need to be sorted in the template. Index in this case
* doesn't have any purpose unless you process it in the template.
* ],
* ],
* ];
*/
"hobbyhome.getLinkList" => function ($links = [], $linkLocation = "primary") {
$newLinks = [];
# ---------------------------------------------------------------------------------
# Replace this section with any method of generating the $newLinks[] array.
# ---------------------------------------------------------------------------------
$pages = site()->index(true)->filterBy("linkLocation", $linkLocation);
foreach ($pages as $page) {
if ($page->isDraft() && !($user = kirby()->user() and $user->role()->isAdmin())) {
continue;
}
$newLinks[] = [
"index" => $page->linkIndex()->toInt(),
"url" => $page->url(),
"icon" => ($page->linkIcon()->isNotEmpty()) ? kirbytag("fa", $page->linkIcon()) : "",
"text" => ($page->linkText()->isNotEmpty()) ? $page->linkText() : $page->title(),
"aria" => ($page->linkText()->isNotEmpty()) ? $page->linkText() : $page->title(),
"active" => $page->isActive(),
];
}
# ---------------------------------------------------------------------------------
foreach ($newLinks as $link) {
$index = $link["index"];
# Make sure no other link exists with the current sort order index.
while (isset($links[$index])) {
$index++;
}
$links[$index] = $link;
}
return $links;
},
], ],
"siteMethods" => [ "siteMethods" => [
/** /**
@@ -62,6 +137,19 @@ Kirby::plugin(
return $files; return $files;
}, },
/**
* Fetch and sort a list of links.
*
* @param string $linkLocation The identifier key for the list of links.
*
* @return Array of links, sorted by index.
*/
"getLinkList" => function ($linkLocation = "primary") {
$links = kirby()->apply("hobbyhome.getLinkList", ["links" => [], "linkLocation" => $linkLocation], "links");
ksort($links, SORT_NUMERIC);
return $links;
},
], ],
], ],
info: [ info: [

View File

@@ -0,0 +1,77 @@
<ul class="navbar-nav me-auto">
<?php foreach ($site->getLinkList("primary") as $link) : ?>
<?php if (isset($link["dropdown"])) : ?>
<?php ksort($link["dropdown"], SORT_NUMERIC) ?>
<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle<?= (isset($link["active"]) and $link["active"]) ? " active" : "" ?><?= (isset($link["disabled"]) and $link["disabled"]) ? " disabled" : "" ?>"
href="#"
data-bs-toggle="dropdown"
title="<?= $link["aria"] ?>"
aria-label="<?= $link["aria"] ?>"
>
<?php if ($link["icon"]) : ?>
<?= $link["icon"] ?>
<span class="d-md-none">
<?php else : ?>
<span>
<?php endif ?>
<?= $link["text"] ?>
</span>
</a>
<ul class="dropdown-menu">
<?php foreach ($link["dropdown"] as $link) : ?>
<li>
<?php if (isset($link["divider"])) : ?>
<hr class="dropdown-divider">
<?php else : ?>
<a
class="dropdown-item<?= (isset($link["active"]) and $link["active"]) ? " active" : "" ?><?= (isset($link["disabled"]) and $link["disabled"]) ? " disabled" : "" ?>"
href="<?= $link["url"] ?>"
<?= isset($link["target"]) ? " target=\"{$link["target"]}\"" : "" ?>
title="<?= $link["aria"] ?>"
aria-label="<?= $link["aria"] ?>"
>
<?php if ($link["icon"]) : ?>
<?= $link["icon"] ?>
<span class="d-md-none">
<?php else : ?>
<span>
<?php endif ?>
<?= $link["text"] ?>
</span>
</a>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
</li>
<?php else : ?>
<li class="nav-item">
<a
class="nav-link<?= (isset($link["active"]) and $link["active"]) ? " active" : "" ?><?= (isset($link["disabled"]) and $link["disabled"]) ? " disabled" : "" ?>"
href="<?= $link["url"] ?>"
<?= isset($link["target"]) ? " target=\"{$link["target"]}\"" : "" ?>
title="<?= $link["aria"] ?>"
aria-label="<?= $link["aria"] ?>"
>
<?php if ($link["icon"]) : ?>
<?= $link["icon"] ?>
<span class="d-md-none">
<?php else : ?>
<span>
<?php endif ?>
<?= $link["text"] ?>
</span>
</a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>