Layoutvertrag (ADR-38) in der bestehenden Registry: Jede Widget-Art erklärt zulässige Rasterbreiten (supportedUnits), Höhen aus einem gemeinsamen Vokabular (heights, gespeichert als settings.height), Darstellungsmodi und Verhalten bei wenig Platz. Unzulässige Breiten werden beim Speichern verständlich abgewiesen; Höhe „fill“ streckt den Handlungsaufruf auf die Abschnittszeile. Premium-Widgets nach Brand-UI-Kit v1.6: - hero Version 2: photo_split/editorial_solid, Höhen compact/normal/ large, Label, Überschriftenebene, Akzent, Bildfokus in Prozent, zwei Schaltflächen; nur 3 oder 4 Einheiten. Upgrade aus Version 1. - image_text Version 3: Fläche cream/clear/navy, Höhen compact/normal/ spacious, Label, zwei Schaltflächen, Bildfokus; 1 bis 4 Einheiten. Upgrade aus Version 2 (format entfällt, Fokus in Prozent). - Kit-CSS ersetzt das bisherige Hero- und Bild-und-Text-CSS; der Randlos-Mechanismus der Designiteration 1 entfällt. Editor: Reiter Inhalt/Medien/Darstellung aus der Registry, Auswahl- knöpfe mit Symbolen, Schieberegler für den Bildfokus und eine Live- Vorschau im Dialog mit demselben Renderer in wählbarer Platzbreite. Tests bewusst angepasst: Einstieg braucht jetzt 3 oder 4 Einheiten (vorher Mindestbreite 2), image_text ist Version 3, Klassen der neuen Ansichten; die Hero-/Bild-Tests der Designiteration 1 sind durch CmsPremiumWidgetsTest ersetzt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
201 lines
7.4 KiB
PHP
201 lines
7.4 KiB
PHP
<?php
|
||
|
||
namespace App\Domain\Cms\Widgets\Support;
|
||
|
||
use App\Domain\Cms\Sections\SectionStyle;
|
||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||
use Filament\Forms\Components\Slider;
|
||
use Filament\Forms\Components\TextInput;
|
||
use Filament\Schemas\Components\Grid;
|
||
|
||
/**
|
||
* Gemeinsame Bausteine der Premium-Widgets (Einstieg, Bild und Text; Brand-UI-Kit v1.6): Label, Überschriftenebene,
|
||
* Bildfokus in Prozent und zwei Schaltflächen. Alle Werte bleiben in data bzw. settings der bestehenden Hülle.
|
||
*
|
||
* Abweichungen vom Kit (bewusst, dokumentiert in cms-widget-registry.md):
|
||
* - Bildfokus als data.focus_x / data.focus_y (ganze Zahlen 0–100), weil settings nur Aufzählungswerte trägt.
|
||
* - Überschriftenebene „Automatisch“ statt freier H1: Die Seite hat genau eine H1 (erstes Widget der Seite).
|
||
* - Linkziele über SafeUrl des Repositorys (Seitenpfad, #Anker, http und https), nicht nur https.
|
||
*/
|
||
final class PremiumFields
|
||
{
|
||
public const HEADING_LEVELS = [
|
||
'auto' => 'Automatisch (Seiteneinstieg H1, sonst H2)',
|
||
'h2' => 'H2',
|
||
'h3' => 'H3',
|
||
];
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
public static function rules(): array
|
||
{
|
||
return [
|
||
'eyebrow' => ['nullable', 'string', 'max:80'],
|
||
'cta_label' => ['nullable', 'string', 'max:80', 'required_with:data.cta_url'],
|
||
'cta_url' => ['nullable', 'string', 'max:500', 'required_with:data.cta_label'],
|
||
'cta2_label' => ['nullable', 'string', 'max:80', 'required_with:data.cta2_url'],
|
||
'cta2_url' => ['nullable', 'string', 'max:500', 'required_with:data.cta2_label'],
|
||
'alt' => ['nullable', 'string', 'max:300'],
|
||
'focus_x' => ['nullable', 'numeric', 'between:0,100'],
|
||
'focus_y' => ['nullable', 'numeric', 'between:0,100'],
|
||
];
|
||
}
|
||
|
||
public static function eyebrowField(): TextInput
|
||
{
|
||
return TextInput::make('eyebrow')
|
||
->label('Label über der Überschrift (optional)')
|
||
->maxLength(80)
|
||
->live(onBlur: true)
|
||
->helperText('Kurzer Hinweis, zum Beispiel ein Themenbereich. Kein Pflichtfeld.');
|
||
}
|
||
|
||
public static function ctaFields(): Grid
|
||
{
|
||
$url = fn (string $name, string $label, string $pair) => TextInput::make($name)
|
||
->label($label)
|
||
->maxLength(500)
|
||
->rule(SafeUrl::rule())
|
||
->requiredWith($pair)
|
||
->live(onBlur: true)
|
||
->placeholder('/programme');
|
||
|
||
return Grid::make(['default' => 1, 'md' => 2])->schema([
|
||
TextInput::make('cta_label')->label('Hauptschaltfläche: Beschriftung')->maxLength(80)->live(onBlur: true),
|
||
$url('cta_url', 'Hauptschaltfläche: Linkziel', 'cta_label'),
|
||
TextInput::make('cta2_label')->label('Zweite Schaltfläche: Beschriftung')->maxLength(80)->live(onBlur: true),
|
||
$url('cta2_url', 'Zweite Schaltfläche: Linkziel', 'cta2_label'),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* @return list<Slider>
|
||
*/
|
||
public static function focusFields(int $defaultX = 50, int $defaultY = 50): array
|
||
{
|
||
// Ohne gespeicherten Wert zeigt der Regler die Vorgabe der Widget-Art, die auch die Darstellung verwendet.
|
||
$slider = fn (string $name, string $label, int $default) => Slider::make($name)
|
||
->label($label)
|
||
->range(0, 100)
|
||
->step(1)
|
||
->required(false)
|
||
->tooltips()
|
||
->default($default)
|
||
->afterStateHydrated(function (Slider $component, mixed $state) use ($default): void {
|
||
if (! is_numeric($state)) {
|
||
$component->state($default);
|
||
}
|
||
})
|
||
->live();
|
||
|
||
return [
|
||
$slider('focus_x', 'Bildausschnitt waagerecht (0 = links, 100 = rechts)', $defaultX),
|
||
$slider('focus_y', 'Bildausschnitt senkrecht (0 = oben, 100 = unten)', $defaultY),
|
||
];
|
||
}
|
||
|
||
public static function altField(): TextInput
|
||
{
|
||
return TextInput::make('alt')
|
||
->label('Alternativtext des Bildes')
|
||
->maxLength(300)
|
||
->live(onBlur: true)
|
||
->helperText('Beschreibt, was auf dem Bild zu sehen ist. Pflicht, sobald ein Bild gewählt ist.');
|
||
}
|
||
|
||
/**
|
||
* Bereinigt Label, Schaltflächen, Alternativtext und Fokus; meldet unsichere Linkziele.
|
||
*
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
public static function clean(array $data, WidgetErrors $errors, bool $hasImage): array
|
||
{
|
||
foreach (['eyebrow', 'cta_label', 'cta_url', 'cta2_label', 'cta2_url', 'alt'] as $key) {
|
||
$data[$key] = filled($data[$key] ?? null) ? trim((string) $data[$key]) : null;
|
||
}
|
||
|
||
foreach (['cta_url', 'cta2_url'] as $key) {
|
||
if ($data[$key] !== null && ! SafeUrl::isSafe($data[$key])) {
|
||
$errors->add("data.$key", SafeUrl::message());
|
||
}
|
||
}
|
||
|
||
foreach (['focus_x', 'focus_y'] as $key) {
|
||
$value = $data[$key] ?? null;
|
||
$data[$key] = is_numeric($value) ? (int) round(max(0, min(100, (float) $value))) : null;
|
||
}
|
||
|
||
if ($hasImage && $data['alt'] === null) {
|
||
$errors->add('data.alt', 'Bitte einen Alternativtext für das Bild angeben.');
|
||
}
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* Schaltflächen mit sicherem Linkziel in Anzeigereihenfolge.
|
||
*
|
||
* @param array<string, mixed> $data
|
||
* @return list<array{label: string, url: string}>
|
||
*/
|
||
public static function actions(array $data): array
|
||
{
|
||
$actions = [];
|
||
|
||
foreach ([['cta_label', 'cta_url'], ['cta2_label', 'cta2_url']] as [$label, $url]) {
|
||
if (filled($data[$label] ?? null) && SafeUrl::isSafe($data[$url] ?? null)) {
|
||
$actions[] = ['label' => (string) $data[$label], 'url' => (string) $data[$url]];
|
||
}
|
||
}
|
||
|
||
return $actions;
|
||
}
|
||
|
||
/**
|
||
* Bildfokus in Prozent; ohne Angabe die Vorgabe der Widget-Art.
|
||
*
|
||
* @param array<string, mixed> $data
|
||
* @return array{x: int, y: int}
|
||
*/
|
||
public static function focus(array $data, int $defaultX = 50, int $defaultY = 50): array
|
||
{
|
||
$clamp = fn (mixed $value, int $default): int => is_numeric($value) ? (int) round(max(0, min(100, (float) $value))) : $default;
|
||
|
||
return ['x' => $clamp($data['focus_x'] ?? null, $defaultX), 'y' => $clamp($data['focus_y'] ?? null, $defaultY)];
|
||
}
|
||
|
||
/**
|
||
* Übersetzt den früheren Fokuspunkt (Aufzählung wie beim Abschnittshintergrund) in Prozentwerte.
|
||
*
|
||
* @return array{focus_x: int|null, focus_y: int|null}
|
||
*/
|
||
public static function focusFromEnum(mixed $focus): array
|
||
{
|
||
if (! is_string($focus) || ! isset(SectionStyle::FOCUS_POSITIONS[$focus])) {
|
||
return ['focus_x' => null, 'focus_y' => null];
|
||
}
|
||
|
||
[$x, $y] = array_map(fn (string $part): int => (int) rtrim($part, '%'), explode(' ', SectionStyle::FOCUS_POSITIONS[$focus]));
|
||
|
||
return ['focus_x' => $x, 'focus_y' => $y];
|
||
}
|
||
|
||
/**
|
||
* Überschriftenebene: „auto“ ergibt H1 für das erste Widget der Seite, sonst H2. Eine weitere H1 gibt es nicht.
|
||
*
|
||
* @param array<string, mixed> $settings
|
||
*/
|
||
public static function headingTag(array $settings, bool $first): string
|
||
{
|
||
$level = $settings['heading_level'] ?? 'auto';
|
||
|
||
return match (true) {
|
||
$first => 'h1',
|
||
$level === 'h3' => 'h3',
|
||
default => 'h2',
|
||
};
|
||
}
|
||
}
|