Biete in allen Widget-Linkfeldern einen Dialog für interne Seiten und Sprungmarken an
Zweistufig (Seite, dann Sprungmarke) mit Veröffentlichungszustand und den noch ungespeicherten Marken der bearbeiteten Seite. Schreibt nur die Adresse ins Feld. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ed53bb1669
commit
6172364bf8
13 files changed
+706
-19
No files matched your search
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms;
|
||||
|
||||
use App\Domain\Cms\Sections\PageSections;
|
||||
|
||||
/**
|
||||
* Interne Linkziele für die Auswahl im CMS: Seiten mit ihrem Veröffentlichungszustand und die Sprungmarken ihrer
|
||||
* Abschnitte. Rein lesend; das Ergebnis ist eine Adresse, die in ein Linkfeld oder einen Menüeintrag geschrieben wird.
|
||||
*
|
||||
* Adressformen: „/pfad“, „/pfad#marke“ (andere Seite) und „#marke“ (dieselbe Seite). Sie bestehen SafeUrl.
|
||||
*/
|
||||
class LinkTargets
|
||||
{
|
||||
public function __construct(private readonly PageSections $sections) {}
|
||||
|
||||
/**
|
||||
* Seiten für die Auswahl, nach Adresse sortiert. Suche in Titel und Adresse.
|
||||
*
|
||||
* @return list<array{id: int, titel: string, pfad: string, zustand: array{label: string, ton: string}, marken: int}>
|
||||
*/
|
||||
public function pages(string $search = '', ?int $exceptId = null): array
|
||||
{
|
||||
$needle = mb_strtolower(trim($search));
|
||||
|
||||
return Page::query()->with(['latestRevision', 'publishedRevision'])->orderBy('path')->get()
|
||||
->reject(fn (Page $page): bool => $page->getKey() === $exceptId)
|
||||
->map(fn (Page $page): array => [
|
||||
'id' => (int) $page->getKey(),
|
||||
'titel' => $page->latestRevision?->contentTitle() ?: $page->path,
|
||||
'pfad' => $page->path,
|
||||
'zustand' => $this->state($page),
|
||||
'marken' => count($this->anchorsOf($page->latestRevision?->sections() ?? [])),
|
||||
])
|
||||
->filter(fn (array $page): bool => $needle === '' || str_contains(mb_strtolower($page['titel'].' '.$page['pfad']), $needle))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{label: string, ton: string}
|
||||
*/
|
||||
public function state(Page $page): array
|
||||
{
|
||||
if ($page->published_revision_id === null) {
|
||||
return ['label' => 'Nur Entwurf, öffentlich nicht erreichbar', 'ton' => 'offen'];
|
||||
}
|
||||
|
||||
return $page->latestRevision !== null && $page->latestRevision->id !== $page->published_revision_id
|
||||
? ['label' => 'Veröffentlicht · Entwurf neuer', 'ton' => 'gut']
|
||||
: ['label' => 'Veröffentlicht', 'ton' => 'gut'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprungmarken einer gespeicherten Seite: die des neuesten Entwurfs, je mit dem Hinweis, ob sie schon
|
||||
* veröffentlicht sind und ob der Abschnitt ausgeblendet ist.
|
||||
*
|
||||
* @return list<array{anchor: string, label: string, hidden: bool, published: bool}>
|
||||
*/
|
||||
public function anchorsForPage(Page $page): array
|
||||
{
|
||||
$published = collect($this->anchorsOf($page->publishedRevision?->sections() ?? []))
|
||||
->filter(fn (array $anchor): bool => ! $anchor['hidden'])
|
||||
->pluck('anchor')
|
||||
->all();
|
||||
|
||||
return array_map(
|
||||
fn (array $anchor): array => $anchor + ['published' => in_array($anchor['anchor'], $published, true)],
|
||||
$this->anchorsOf($page->latestRevision?->sections() ?? $page->publishedRevision?->sections() ?? []),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprungmarken der Abschnitte im Editor, auch ungespeicherte. Für „diese Seite“.
|
||||
*
|
||||
* @param list<array<string, mixed>> $sections Abschnitte im Modell (nach SectionEditor::fromEditorState)
|
||||
* @return list<array{anchor: string, label: string, hidden: bool, published: bool}>
|
||||
*/
|
||||
public function anchorsInEditor(array $sections): array
|
||||
{
|
||||
return array_map(fn (array $anchor): array => $anchor + ['published' => false], $this->anchorsOf($sections));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string, mixed>> $sections
|
||||
* @return list<array{anchor: string, label: string, hidden: bool}>
|
||||
*/
|
||||
public function anchorsOf(array $sections): array
|
||||
{
|
||||
$anchors = [];
|
||||
|
||||
foreach (array_values($sections) as $position => $section) {
|
||||
$anchor = PageSections::cleanAnchor($section['anchor'] ?? null);
|
||||
|
||||
if ($anchor === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = PageSections::cleanName($section['name'] ?? null);
|
||||
$anchors[] = [
|
||||
'anchor' => $anchor,
|
||||
'label' => $name ?? 'Abschnitt '.($position + 1),
|
||||
'hidden' => PageSections::isHidden($section['hidden'] ?? false),
|
||||
];
|
||||
}
|
||||
|
||||
return $anchors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adresse für ein Linkfeld: „/pfad“, „/pfad#marke“ oder (dieselbe Seite) „#marke“.
|
||||
*/
|
||||
public function href(?string $path, ?string $anchor): ?string
|
||||
{
|
||||
$anchor = $anchor !== null && $anchor !== '' ? PageSections::cleanAnchor($anchor) : null;
|
||||
|
||||
if ($path === null) {
|
||||
return $anchor !== null ? '#'.$anchor : null;
|
||||
}
|
||||
|
||||
return $path.($anchor !== null ? '#'.$anchor : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Vollständige öffentliche Adresse mit Sprungmarke, für Verweise von außen.
|
||||
*/
|
||||
public function publicUrl(Page $page, ?string $anchor = null): string
|
||||
{
|
||||
return $page->publicUrl().($anchor !== null && $anchor !== '' ? '#'.$anchor : '');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms\Widgets\Support;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Illuminate\Support\Js;
|
||||
|
||||
/**
|
||||
* Linkfeld für alle Widgets: Die Adresse lässt sich frei eintippen (Seitenpfad, #Sprungmarke oder http(s)-Adresse) oder
|
||||
* über den Dialog „Interne Seite oder Sprungmarke wählen“ aus den Seiten der Website übernehmen
|
||||
* (ChoosesLinkTargets). Der Knopf ruft den Dialog direkt auf; ein Aktionsdialog im Bausteindialog entsteht nicht. Wo die
|
||||
* Seite den Dialog nicht kennt, fehlt der Knopf und das Feld bleibt ein Textfeld.
|
||||
*/
|
||||
final class LinkField
|
||||
{
|
||||
public static function make(string $name, string $label = 'Linkziel'): TextInput
|
||||
{
|
||||
return TextInput::make($name)
|
||||
->label($label)
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
->suffixAction(Action::make('linkWaehlen')
|
||||
->label('Interne Seite oder Sprungmarke wählen')
|
||||
->tooltip('Interne Seite oder Sprungmarke wählen')
|
||||
->icon('heroicon-o-link')
|
||||
->visible(fn ($livewire): bool => is_object($livewire) && method_exists($livewire, 'openLinkPicker'))
|
||||
->actionJs(fn ($component): string => '$wire.openLinkPicker('.Js::from($component->getStatePath()).')'));
|
||||
}
|
||||
}
|
||||
@@ -53,10 +53,7 @@ public static function eyebrowField(): TextInput
|
||||
|
||||
public static function ctaFields(): Grid
|
||||
{
|
||||
$url = fn (string $name, string $label, string $pair) => TextInput::make($name)
|
||||
->label($label)
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
$url = fn (string $name, string $label, string $pair) => LinkField::make($name, $label)
|
||||
->requiredWith($pair)
|
||||
->live(onBlur: true)
|
||||
->placeholder('/programme');
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Domain\Cms\Widgets\Types;
|
||||
|
||||
use App\Domain\Cms\Widgets\Support\HeadingAccent;
|
||||
use App\Domain\Cms\Widgets\Support\LinkField;
|
||||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetType;
|
||||
@@ -82,12 +83,9 @@ public function dataFields(): array
|
||||
TextInput::make('heading')->label('Überschrift')->required()->maxLength(200),
|
||||
Textarea::make('text')->label('Text')->rows(3)->maxLength(1000),
|
||||
TextInput::make('label')->label('Beschriftung der Schaltfläche')->required()->maxLength(80),
|
||||
TextInput::make('url')
|
||||
->label('Linkziel')
|
||||
LinkField::make('url')
|
||||
->required()
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
->helperText('Seitenpfad wie /angebote oder eine vollständige http(s)-Adresse.'),
|
||||
->helperText('Seitenpfad wie /angebote, Sprungmarke wie #kurse oder eine vollständige http(s)-Adresse; oder über das Linksymbol eine Seite oder Sprungmarke wählen.'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Support\HeadingAccent;
|
||||
use App\Domain\Cms\Widgets\Support\ImageRefFields;
|
||||
use App\Domain\Cms\Widgets\Support\LinkField;
|
||||
use App\Domain\Cms\Widgets\Support\PublicMedia;
|
||||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||||
use App\Domain\Cms\Widgets\Support\WebsiteImage;
|
||||
@@ -127,11 +128,8 @@ public function dataFields(): array
|
||||
SymbolField::make('symbol', 'Symbol (optional)')
|
||||
->helperText('Kleine Grafik aus dem Brand-Kit über dem Titel. Größe und Ausrichtung im Reiter „Darstellung“.'),
|
||||
Textarea::make('text')->label('Beschreibung')->rows(2)->maxLength(500),
|
||||
TextInput::make('url')
|
||||
->label('Linkziel')
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
->helperText('Seitenpfad wie /praevention oder eine http(s)-Adresse. Leer lassen für eine Karte ohne Link.'),
|
||||
LinkField::make('url')
|
||||
->helperText('Seitenpfad wie /praevention, Sprungmarke wie #kurse oder eine http(s)-Adresse; oder über das Linksymbol wählen. Leer lassen für eine Karte ohne Link.'),
|
||||
$image,
|
||||
$upload,
|
||||
$status,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Support\HeadingAccent;
|
||||
use App\Domain\Cms\Widgets\Support\LinkField;
|
||||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetType;
|
||||
@@ -157,11 +158,8 @@ public function dataFields(): array
|
||||
SymbolField::make('symbol', 'Symbol', required: true),
|
||||
TextInput::make('title')->label('Kurztitel')->required()->maxLength(60),
|
||||
Textarea::make('text')->label('Satz')->rows(2)->maxLength(200),
|
||||
TextInput::make('url')
|
||||
->label('Linkziel (optional)')
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
->helperText('Seitenpfad wie /materialien oder eine http(s)-Adresse. Leer lassen für ein Merkmal ohne Link.'),
|
||||
LinkField::make('url', 'Linkziel (optional)')
|
||||
->helperText('Seitenpfad wie /materialien, Sprungmarke wie #kurse oder eine http(s)-Adresse; oder über das Linksymbol wählen. Leer lassen für ein Merkmal ohne Link.'),
|
||||
TextInput::make('link_label')
|
||||
->label('Linktext')
|
||||
->maxLength(60)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\PageService;
|
||||
use App\Filament\Resources\Pages\PageResource;
|
||||
use App\Filament\Support\ChoosesLinkTargets;
|
||||
use App\Filament\Support\ChoosesWebsiteImages;
|
||||
use App\Filament\Support\ReportsSectionErrors;
|
||||
use App\Filament\Support\SectionEditor;
|
||||
@@ -18,6 +19,7 @@
|
||||
|
||||
class CreatePage extends CreateRecord
|
||||
{
|
||||
use ChoosesLinkTargets;
|
||||
use ChoosesWebsiteImages;
|
||||
use ReportsSectionErrors;
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\PageService;
|
||||
use App\Filament\Resources\Pages\PageResource;
|
||||
use App\Filament\Support\ChoosesLinkTargets;
|
||||
use App\Filament\Support\ChoosesWebsiteImages;
|
||||
use App\Filament\Support\ReportsSectionErrors;
|
||||
use App\Filament\Support\SectionEditor;
|
||||
@@ -22,6 +23,7 @@
|
||||
|
||||
class EditPage extends EditRecord
|
||||
{
|
||||
use ChoosesLinkTargets;
|
||||
use ChoosesWebsiteImages;
|
||||
use ReportsSectionErrors;
|
||||
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support;
|
||||
|
||||
use App\Domain\Cms\LinkTargets;
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||||
|
||||
/**
|
||||
* Auswahl interner Linkziele im CMS über einen Dialog (filament.cms.link-picker): erst die Seite, dann auf Wunsch eine
|
||||
* Sprungmarke. Ein Linkfeld (Widgets\Support\LinkField) öffnet den Dialog mit seinem Formularpfad; „übernehmen“ schreibt die Adresse in
|
||||
* genau dieses Feld, auch im geöffneten Bausteindialog. Gespeichert wird weiterhin nur mit dem Seitenentwurf. Die
|
||||
* Auswahl ändert keine Seite und veröffentlicht nichts.
|
||||
*
|
||||
* Die Sprungmarken der Seite, die gerade bearbeitet wird, stammen aus dem Formular und enthalten damit auch
|
||||
* ungespeicherte Marken.
|
||||
*/
|
||||
trait ChoosesLinkTargets
|
||||
{
|
||||
/** Formularpfad des Linkfelds, in das die Adresse übernommen wird; null = Dialog geschlossen. */
|
||||
public ?string $linkZiel = null;
|
||||
|
||||
public string $linkSuche = '';
|
||||
|
||||
/** Gewählte Seite im zweiten Schritt: „diese“ (Seite im Editor) oder die Kennung einer Seite. */
|
||||
public ?string $linkSeite = null;
|
||||
|
||||
public function openLinkPicker(string $pfad): void
|
||||
{
|
||||
if (preg_match('/^(data|mountedActions\.\d+\.data)(\.[A-Za-z0-9_-]+)+$/', $pfad) !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->linkZiel = $pfad;
|
||||
$this->linkSuche = '';
|
||||
$this->linkSeite = null;
|
||||
$this->forceRenderForLinkPicker();
|
||||
}
|
||||
|
||||
public function closeLinkPicker(): void
|
||||
{
|
||||
$this->linkZiel = null;
|
||||
$this->linkSeite = null;
|
||||
$this->linkSuche = '';
|
||||
$this->forceRenderForLinkPicker();
|
||||
}
|
||||
|
||||
public function chooseLinkPage(string $key): void
|
||||
{
|
||||
if ($this->linkZiel === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($key === 'diese' ? ! $this->linkHasOwnPage() : ! Page::query()->whereKey((int) $key)->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->linkSeite = $key;
|
||||
}
|
||||
|
||||
public function backToLinkPages(): void
|
||||
{
|
||||
$this->linkSeite = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ?string $anchor Sprungmarke; leer = Seitenanfang (nur für andere Seiten)
|
||||
*/
|
||||
public function applyLink(?string $anchor = null): void
|
||||
{
|
||||
if ($this->linkZiel === null || $this->linkSeite === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targets = app(LinkTargets::class);
|
||||
$anchor = filled($anchor) ? $anchor : null;
|
||||
|
||||
if ($this->linkSeite === 'diese') {
|
||||
$known = array_column($targets->anchorsInEditor($this->linkEditorSections()), 'anchor');
|
||||
|
||||
if ($anchor === null || ! in_array($anchor, $known, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$href = $targets->href(null, $anchor);
|
||||
} else {
|
||||
$page = Page::query()->with(['latestRevision', 'publishedRevision'])->find((int) $this->linkSeite);
|
||||
|
||||
if (! $page instanceof Page) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($anchor !== null && ! in_array($anchor, array_column($targets->anchorsForPage($page), 'anchor'), true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$href = $targets->href($page->path, $anchor);
|
||||
}
|
||||
|
||||
if (! SafeUrl::isSafe($href)) {
|
||||
return;
|
||||
}
|
||||
|
||||
data_set($this, $this->linkZiel, $href);
|
||||
$this->closeLinkPicker();
|
||||
}
|
||||
|
||||
/**
|
||||
* Seiten im ersten Schritt (ohne die bearbeitete Seite, die als „Diese Seite“ oben steht).
|
||||
*
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public function linkPages(): array
|
||||
{
|
||||
return app(LinkTargets::class)->pages($this->linkSuche, $this->linkOwnPageId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Daten des zweiten Schritts: gewählte Seite, ihre Sprungmarken und die vollständige Adresse.
|
||||
*
|
||||
* @return array{titel: string, pfad: ?string, diese: bool, marken: list<array<string, mixed>>, offen: bool, adresse: ?string}|null
|
||||
*/
|
||||
public function linkChosen(): ?array
|
||||
{
|
||||
if ($this->linkSeite === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$targets = app(LinkTargets::class);
|
||||
|
||||
if ($this->linkSeite === 'diese') {
|
||||
$record = $this->linkOwnPage();
|
||||
|
||||
return [
|
||||
'titel' => 'Diese Seite',
|
||||
'pfad' => $record?->path,
|
||||
'diese' => true,
|
||||
'marken' => $targets->anchorsInEditor($this->linkEditorSections()),
|
||||
'offen' => $record === null,
|
||||
'adresse' => $record?->published_revision_id !== null ? $targets->publicUrl($record) : null,
|
||||
];
|
||||
}
|
||||
|
||||
$page = Page::query()->with(['latestRevision', 'publishedRevision'])->find((int) $this->linkSeite);
|
||||
|
||||
if (! $page instanceof Page) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'titel' => $page->latestRevision?->contentTitle() ?: $page->path,
|
||||
'pfad' => $page->path,
|
||||
'diese' => false,
|
||||
'marken' => $targets->anchorsForPage($page),
|
||||
'offen' => false,
|
||||
'adresse' => $page->published_revision_id !== null ? $targets->publicUrl($page) : null,
|
||||
];
|
||||
}
|
||||
|
||||
/** Hat die Seite im Editor schon Sprungmarken oder eine Adresse? Neue Seiten dürfen auf ihre eigenen Marken zeigen. */
|
||||
public function linkHasOwnPage(): bool
|
||||
{
|
||||
return property_exists($this, 'data');
|
||||
}
|
||||
|
||||
public function linkOwnPage(): ?Page
|
||||
{
|
||||
$record = property_exists($this, 'record') ? ($this->record ?? null) : null;
|
||||
|
||||
return $record instanceof Page ? $record : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprungmarken der Seite im Editor (auch ungespeicherte).
|
||||
*
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public function linkOwnAnchors(): array
|
||||
{
|
||||
return $this->linkHasOwnPage() ? app(LinkTargets::class)->anchorsInEditor($this->linkEditorSections()) : [];
|
||||
}
|
||||
|
||||
private function linkOwnPageId(): ?int
|
||||
{
|
||||
return $this->linkOwnPage()?->getKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function linkEditorSections(): array
|
||||
{
|
||||
$state = is_array($this->data['sections'] ?? null) ? $this->data['sections'] : [];
|
||||
|
||||
return SectionEditor::fromEditorState($state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filament rendert bei offenen Aktionsdialogen nur den Dialogteil neu; der Auswahldialog liegt darüber und braucht
|
||||
* die ganze Seite.
|
||||
*/
|
||||
public function hydrateChoosesLinkTargets(): void
|
||||
{
|
||||
if ($this->linkZiel !== null) {
|
||||
$this->forceRenderForLinkPicker();
|
||||
}
|
||||
}
|
||||
|
||||
private function forceRenderForLinkPicker(): void
|
||||
{
|
||||
if (method_exists($this, 'forceRender')) {
|
||||
$this->forceRender();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user