'Automatisch (Seiteneinstieg H1, sonst H2)', 'h2' => 'H2', 'h3' => 'H3', ]; /** * @return array */ 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 */ 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 $data * @return array */ 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 $data * @return list */ 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 $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 $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', }; } }