Skip to content

Instantly share code, notes, and snippets.

@lukaskleinschmidt
Last active March 1, 2024 15:57
Show Gist options
  • Save lukaskleinschmidt/247a957ebcde66899757a16fead9a039 to your computer and use it in GitHub Desktop.
Save lukaskleinschmidt/247a957ebcde66899757a16fead9a039 to your computer and use it in GitHub Desktop.
Kirby 4 Panel Menu
<?php
use App\Menu;
return [
'panel.menu' => [
'site' => Menu::site('Overview'),
'-',
'magazine' => Menu::page('Magazine', 'magazine', 'pages/magazine'),
'trips' => Menu::page('Trips', 'trips', 'pages/trips'),
'service' => Menu::page('Service', 'service', 'pages/service'),
'-',
'uploads' => Menu::page('Uploads', 'uploads', 'pages/uploads'),
'users',
'-',
'languages',
'system',
],
];
<?php
namespace App;
use Closure;
use Kirby\Cms\App;
class Menu
{
public static array $pages = [];
public static string $path;
public static function path(): string
{
return static::$path ??= App::instance()->request()->path()->toString();
}
public static function page(string $label, string $icon = null, string $link = null, Closure|bool $current = null): array
{
return static::$pages[] = [
'label' => $label,
'link' => $link,
'icon' => $icon,
'current' => $current ?? fn () =>
str_contains(static::path(), $link)
];
}
public static function site(string $label, string $icon = 'home'): array
{
return [
'label' => $label,
'current' => function (string $id = null) {
if ($id !== 'site') {
return false;
}
foreach (static::$pages as &$page) {
if (str_contains(static::path(), $page['link'])) {
return false;
}
}
return true;
},
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment