Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View buttons: i18n + query support, component name from key #6901

Open
wants to merge 5 commits into
base: v5/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 1 addition & 4 deletions src/Panel/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ public function buttons(): array
'preview',
'settings',
'languages'
)->bind([
'file' => $this->model(),
'model' => $this->model()
])->render();
)->render();
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Panel/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public function buttons(): array
'settings',
'languages',
'status'
)->bind([
'page' => $this->model(),
'model' => $this->model()
])->render();
)->render();
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Panel/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public function buttons(): array
return ViewButtons::view($this)->defaults(
'preview',
'languages'
)->bind([
'model' => $this->model(),
'site' => $this->model()
])->render();
)->render();
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Panel/Ui/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kirby\Panel\Ui;

use Kirby\Toolkit\I18n;

/**
* @package Kirby Panel
* @author Bastian Allgeier <[email protected]>
Expand All @@ -28,9 +30,9 @@ public function __construct(
public string|null $size = null,
public string|null $style = null,
public string|null $target = null,
public string|null $text = null,
public string|array|null $text = null,
public string|null $theme = null,
public string|null $title = null,
public string|array|null $title = null,
public string $type = 'button',
public string|null $variant = null,
) {
Expand All @@ -51,9 +53,9 @@ public function props(): array
'responsive' => $this->responsive,
'size' => $this->size,
'target' => $this->target,
'text' => $this->text,
'text' => I18n::translate($this->text, $this->text),
'theme' => $this->theme,
'title' => $this->title,
'title' => I18n::translate($this->title, $this->title),
'type' => $this->type,
'variant' => $this->variant,
];
Expand Down
5 changes: 3 additions & 2 deletions src/Panel/Ui/Buttons/LanguagesDropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ class LanguagesDropdown extends ViewButton
protected App $kirby;

public function __construct(
protected ModelWithContent $model
ModelWithContent $model
) {
$this->kirby = $model->kirby();

parent::__construct(
component: 'k-languages-dropdown',
model: $model,
class: 'k-languages-dropdown',
icon: 'translate',
// Fiber dropdown endpoint to load options
// only when dropdown is opened
options: $this->model->panel()->url(true) . '/languages',
options: $model->panel()->url(true) . '/languages',
responsive: 'text',
text: Str::upper($this->kirby->language()?->code())
);
Expand Down
53 changes: 47 additions & 6 deletions src/Panel/Ui/Buttons/ViewButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Closure;
use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Cms\ModelWithContent;
use Kirby\Panel\Panel;
use Kirby\Panel\Ui\Button;
use Kirby\Toolkit\Controller;
Expand All @@ -24,6 +26,7 @@ class ViewButton extends Button
{
public function __construct(
public string $component = 'k-view-button',
public readonly ModelWithContent|Language|null $model = null,
public array|null $badge = null,
public string|null $class = null,
public string|bool|null $current = null,
Expand All @@ -38,9 +41,9 @@ public function __construct(
public string|null $size = 'sm',
public string|null $style = null,
public string|null $target = null,
public string|null $text = null,
public string|array|null $text = null,
public string|null $theme = null,
public string|null $title = null,
public string|array|null $title = null,
public string $type = 'button',
public string|null $variant = 'filled',
) {
Expand All @@ -53,15 +56,18 @@ public function __construct(
*/
public static function factory(
string|array|Closure $button,
string|int|null $name = null,
string|null $view = null,
ModelWithContent|Language|null $model = null,
array $data = []
): static|null {
// referenced by name
if (is_string($button) === true) {
$button = static::find($button, $view);
}

$button = static::resolve($button, $data);
// turn closure into actual button (object or array)
$button = static::resolve($button, $model, $data);

if (
$button === null ||
Expand All @@ -70,7 +76,17 @@ public static function factory(
return $button;
}

return new static(...static::normalize($button));
// flatten definition array into list of arguments for this class
$button = static::normalize($button);

// if button definition has a name, use it for the component name
if (is_string($name) === true) {
// If this specific component does not exist,
// `k-view-buttons` will fall back to `k-view-button` again
$button['component'] ??= 'k-' . $name . '-view-button';
}

return new static(...$button, model: $model);
}

/**
Expand Down Expand Up @@ -125,8 +141,20 @@ public static function normalize(array $button): array

public function props(): array
{
// helper for props that support Kirby queries
$resolve = fn ($value) =>
$value ?
$this->model?->toSafeString($value) ?? $value :
null;

return [
...parent::props(),
...$props = parent::props(),
'dialog' => $resolve($props['dialog']),
'drawer' => $resolve($props['drawer']),
'icon' => $resolve($props['icon']),
'link' => $resolve($props['link']),
'text' => $resolve($props['text']),
'theme' => $resolve($props['theme']),
'options' => $this->options
];
}
Expand All @@ -138,12 +166,25 @@ public function props(): array
*/
public static function resolve(
Closure|array $button,
ModelWithContent|Language|null $model = null,
array $data = []
): static|array|null {
if ($button instanceof Closure) {
$kirby = App::instance();
$controller = new Controller($button);
$button = $controller->call(data: [

if (
$model instanceof ModelWithContent ||
$model instanceof Language
) {
$data = [
'model' => $model,
$model::CLASS_ALIAS => $model,
...$data
];
}

$button = $controller->call(data: [
'kirby' => $kirby,
'site' => $kirby->site(),
'user' => $kirby->user(),
Expand Down
40 changes: 25 additions & 15 deletions src/Panel/Ui/Buttons/ViewButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Kirby\Panel\Ui\Buttons;

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

/**
* Collects view buttons for a specific view
Expand All @@ -21,9 +22,12 @@ class ViewButtons
{
public function __construct(
public readonly string $view,
public readonly ModelWithContent|Language|null $model = null,
public array|false|null $buttons = null,
public array $data = []
) {
// if no specific buttons are passed,
// use default buttons for this view from config
$this->buttons ??= App::instance()->option(
'panel.viewButtons.' . $view
);
Expand Down Expand Up @@ -62,32 +66,38 @@ public function render(): array
return [];
}

$buttons = A::map(
$this->buttons ?? [],
fn ($button) =>
ViewButton::factory(
$button,
$this->view,
$this->data
)?->render()
);
$buttons = [];

foreach ($this->buttons ?? [] as $name => $button) {
$buttons[] = ViewButton::factory(
button: $button,
name: $name,
view: $this->view,
model: $this->model,
data: $this->data
)?->render();
}

return array_values(array_filter($buttons));
return array_filter($buttons);
}

/**
* Creates new instance for a view
* with special support for model views
*/
public static function view(string|Model $view): static
{
public static function view(
string|Model $view,
ModelWithContent|Language|null $model = null
): static {
if ($view instanceof Model) {
$blueprint = $view->model()->blueprint()->buttons();
$view = $view->model()::CLASS_ALIAS;
$model = $view->model();
$blueprint = $model->blueprint()->buttons();
$view = $model::CLASS_ALIAS;
}

return new static(
view: $view,
model: $model ?? null,
buttons: $blueprint ?? null
);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Panel/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ public function buttons(): array
'theme',
'settings',
'languages'
)->bind([
'model' => $this->model(),
'user' => $this->model()
])->render();
)->render();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Panel/Ui/ButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@ public function testProps()
'variant' => 'filled',
], $component->props());
}

/**
* @covers ::props
*/
public function testPropsWithI18n()
{
$component = new Button(
text: [
'en' => 'Congrats',
'de' => 'Glückwunsch'
],
);

$props = $component->props();
$this->assertSame('Congrats', $props['text']);
}
}
Loading
Loading