Skip to content

Commit

Permalink
Simplify menu item creation
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Feb 3, 2025
1 parent f9a4799 commit 10a871b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
26 changes: 20 additions & 6 deletions src/Panel/Area.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Panel;

use Closure;
use Kirby\Panel\Ui\MenuItem;
use Kirby\Toolkit\I18n;

/**
Expand Down Expand Up @@ -122,14 +123,14 @@ public function merge(array $props): static
* how the menu item for the area should be rendered
* and if it should be rendered at all
*/
public function menuSettings(
public function menuItem(
array $areas = [],
array $permissions = [],
string|null $current = null
): array|false {
): MenuItem|null {
// areas without access permissions get skipped entirely
if ($this->isAccessible($permissions) === false) {
return false;
return null;
}

$menu = $this->menu;
Expand All @@ -143,14 +144,27 @@ public function menuSettings(
// false will remove the area/entry entirely
// just like with disabled permissions
if ($menu === false) {
return false;
return null;
}

return match ($menu) {
// create a new menu item instance for the area
$item = new MenuItem(
current: $this->isCurrent($current),
icon: $this->icon() ?? $this->id(),
text: $this->label(),
dialog: $this->dialog(),
drawer: $this->drawer(),
link: $this->link(),
);

// add the custom menu settings
$item->merge(match ($menu) {
'disabled' => ['disabled' => true],
true => [],
default => $menu
};
});

return $item;
}

/**
Expand Down
17 changes: 1 addition & 16 deletions src/Panel/Ui/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,11 @@ public function item(Area|null $area): MenuItem|null
return null;
}

$menuSettings = $area->menuSettings(
return $area->menuItem(
areas: $this->areas,
permissions: $this->permissions,
current: $this->current
);

if ($menuSettings === false) {
return null;
}

$item = new MenuItem(
current: $area->isCurrent($this->current),
icon: $area->icon() ?? $area->id(),
text: $area->label(),
dialog: $area->dialog(),
drawer: $area->drawer(),
link: $area->link(),
);

return $item->merge($menuSettings);
}

/**
Expand Down
46 changes: 27 additions & 19 deletions tests/Panel/AreaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirby\Panel;

use Kirby\Panel\Ui\MenuItem;
use Kirby\TestCase;
use Kirby\Toolkit\I18n;

Expand Down Expand Up @@ -296,30 +297,35 @@ public function testMerge()
}

/**
* @covers ::menuSettings
* @covers ::menuItem
*/
public function testMenuSettings()
public function testMenuItem()
{
$area = new Area(id: 'test');

$this->assertFalse($area->menuSettings());
$this->assertNull($area->menuItem());
}

/**
* @covers ::menuSettings
* @covers ::menuItem
*/
public function testMenuSettingsWithEnabledMenu()
public function testMenuItemWithEnabledMenu()
{
$area = new Area(
id: 'test',
menu: true,
);

$this->assertSame([], $area->menuSettings());
$menuItem = $area->menuItem();

$this->assertInstanceOf(MenuItem::class, $menuItem);
$this->assertSame('test', $menuItem->icon());
$this->assertSame('test', $menuItem->text());
$this->assertSame('test', $menuItem->link());
}

/**
* @covers ::menuSettings
* @covers ::menuItem
*/
public function testMenuSettingsWithDisabledAccess()
{
Expand All @@ -328,7 +334,7 @@ public function testMenuSettingsWithDisabledAccess()
menu: true,
);

$this->assertFalse($area->menuSettings(
$this->assertNull($area->menuItem(
permissions: [
'access' => [
'test' => false
Expand All @@ -338,9 +344,9 @@ public function testMenuSettingsWithDisabledAccess()
}

/**
* @covers ::menuSettings
* @covers ::menuItem
*/
public function testMenuSettingsWithClosureDefinition()
public function testMenuItemWithClosureDefinition()
{
$menu = [
'icon' => 'edit'
Expand All @@ -367,39 +373,41 @@ public function testMenuSettingsWithClosureDefinition()
}
);

$this->assertSame($menu, $area->menuSettings(
$menuItem = $area->menuItem(
areas: $passedAreas,
permissions: $passedPermissions,
current: 'test'
));
);

$this->assertSame('edit', $menuItem->icon());
}

/**
* @covers ::menuSettings
* @covers ::menuItem
*/
public function testMenuSettingsWithArrayDefinition()
public function testMenuItemWithArrayDefinition()
{
$area = new Area(
id: 'test',
menu: $menu = [
menu: [
'icon' => 'edit'
]
);

$this->assertSame($menu, $area->menuSettings());
$this->assertSame('edit', $area->menuItem()->icon());
}

/**
* @covers ::menuSettings
* @covers ::menuItem
*/
public function testMenuSettingsWithDisabledFlag()
public function testMenuItemWithDisabledFlag()
{
$area = new Area(
id: 'test',
menu: 'disabled'
);

$this->assertSame(['disabled' => true], $area->menuSettings());
$this->assertTrue($area->menuItem()->disabled());
}

/**
Expand Down

0 comments on commit 10a871b

Please sign in to comment.