Skip to content

Commit

Permalink
ViewButton: support Language as model
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jan 11, 2025
1 parent 8d8aecd commit 0db0596
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 1 addition & 2 deletions config/areas/languages/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@
],
'props' => [
'buttons' => fn () =>
ViewButtons::view('language')
ViewButtons::view('language', model: $language)
->defaults('preview', 'settings', 'delete')
->bind(['language' => $language])
->render(),
'deletable' => $language->isDeletable(),
'code' => Escape::html($language->code()),
Expand Down
5 changes: 5 additions & 0 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Language implements Stringable
*/
use HasSiblings;

/**
* Short human-readable version used in template queries
*/
public const CLASS_ALIAS = 'language';

/**
* The parent Kirby instance
*/
Expand Down
12 changes: 8 additions & 4 deletions src/Panel/Ui/Buttons/ViewButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Cms\ModelWithContent;
use Kirby\Panel\Panel;
use Kirby\Panel\Ui\Button;
Expand All @@ -25,7 +26,7 @@ class ViewButton extends Button
{
public function __construct(
public string $component = 'k-view-button',
public readonly ModelWithContent|null $model = null,
public readonly ModelWithContent|Language|null $model = null,
public array|null $badge = null,
public string|null $class = null,
public string|bool|null $current = null,
Expand Down Expand Up @@ -57,7 +58,7 @@ public static function factory(
string|array|Closure $button,
string|int|null $name = null,
string|null $view = null,
ModelWithContent|null $model = null,
ModelWithContent|Language|null $model = null,
array $data = []
): static|null {
// referenced by name
Expand Down Expand Up @@ -162,14 +163,17 @@ public function props(): array
*/
public static function resolve(
Closure|array $button,
ModelWithContent|null $model = null,
ModelWithContent|Language|null $model = null,
array $data = []
): static|array|null {
if ($button instanceof Closure) {
$kirby = App::instance();
$controller = new Controller($button);

if ($model instanceof ModelWithContent) {
if (
$model instanceof ModelWithContent ||
$model instanceof Language
) {
$data = [
'model' => $model,
$model::CLASS_ALIAS => $model,
Expand Down
5 changes: 3 additions & 2 deletions src/Panel/Ui/Buttons/ViewButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Cms\ModelWithContent;
use Kirby\Panel\Model;

Expand All @@ -21,7 +22,7 @@ class ViewButtons
{
public function __construct(
public readonly string $view,
public readonly ModelWithContent|null $model = null,
public readonly ModelWithContent|Language|null $model = null,
public array|false|null $buttons = null,
public array $data = []
) {
Expand Down Expand Up @@ -86,7 +87,7 @@ public function render(): array
*/
public static function view(
string|Model $view,
ModelWithContent|null $model = null
ModelWithContent|Language|null $model = null
): static {
if ($view instanceof Model) {
$model = $view->model();
Expand Down

0 comments on commit 0db0596

Please sign in to comment.