Skip to content

Commit

Permalink
Panel area buttons extension
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jul 20, 2024
1 parent 3542199 commit ce0b491
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Panel/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Kirby\Http\Router;
use Kirby\Http\Uri;
use Kirby\Http\Url;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\Tpl;
use Throwable;
Expand Down Expand Up @@ -100,6 +101,20 @@ public static function areas(): array
return $result;
}

/**
* Collect all registered buttons from areas
* @since 5.0.0
*/
public static function buttons(): array
{
return array_merge(...array_values(
A::map(
Panel::areas(),
fn ($area) => $area['buttons'] ?? []
)
));
}

/**
* Check for access permissions
*/
Expand Down
1 change: 1 addition & 0 deletions src/Panel/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public static function data(array $view = [], array $options = []): array

// make sure that views and dialogs are gone
unset(
$view['buttons'],
$view['dialogs'],
$view['drawers'],
$view['dropdowns'],
Expand Down
38 changes: 37 additions & 1 deletion tests/Panel/PanelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Kirby\Filesystem\Dir;
use Kirby\Http\Response;
use Kirby\TestCase;
use Kirby\Toolkit\A;

/**
* @coversDefaultClass \Kirby\Panel\Panel
Expand Down Expand Up @@ -131,6 +130,43 @@ public function testAreas(): void
$this->assertCount(8, $areas);
}

/**
* @covers ::buttons
*/
public function testButtons(): void
{
$this->app = $this->app->clone([
'users' => [
[
'email' => '[email protected]',
'role' => 'admin'
]
]
]);

$this->app->impersonate('[email protected]');
$core = Panel::buttons();

// add custom buttons
$this->app = $this->app->clone([
'areas' => [
'test' => fn () => [
'buttons' => [
'a' => ['component' => 'test-a'],
'b' => ['component' => 'test-b']
]
]
]
]);

$this->app->impersonate('[email protected]');
$withCustoms = Panel::buttons();

$this->assertSame(2, count($withCustoms) - count($core));
$this->assertSame(['component' => 'test-b'], array_pop($withCustoms));
$this->assertSame(['component' => 'test-a'], array_pop($withCustoms));
}

/**
* @covers ::firewall
* @covers ::hasAccess
Expand Down

0 comments on commit ce0b491

Please sign in to comment.