Files
fewo-stommer/app/Filament/Forms/Components/PhotoSelect.php
T
LarsandClaude Opus 5.5 791786ed3e Saisonfotos am Foto statt am Erscheinungsbild
Ein Foto gehoert zu keinem (ganzjaehrig) oder mehreren Erscheinungsbildern und
erscheint nur dort, vor den ganzjaehrigen Fotos. Bausteine waehlen "automatisch"
oder eine eigene Auswahl; Titelbereich und Abschluss zeigen das erste passende
Foto ihrer Liste. Titelbild und "Bilder je Baustein" des Erscheinungsbilds werden
in die Bausteine uebernommen und entfallen dort.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 18:53:56 +01:00

48 lines
1.7 KiB
PHP

<?php
namespace App\Filament\Forms\Components;
use App\Models\Photo;
use Filament\Forms\Components\Select;
/**
* Chooses website photos with a thumbnail next to each description, grouped by category,
* so editors see what they insert.
*/
class PhotoSelect extends Select
{
protected function setUp(): void
{
parent::setUp();
$this
->options(fn (): array => self::photoOptions())
->allowHtml()
->searchable()
->native(false);
}
/**
* @return array<string, array<int, string>>
*/
public static function photoOptions(): array
{
return Photo::query()->with('media')->orderBy('category')->orderBy('sort_order')->get()
->groupBy(fn (Photo $photo): string => $photo->category->getLabel())
->map(fn ($photos): array => $photos->mapWithKeys(fn (Photo $photo): array => [$photo->id => self::optionLabel($photo)])->all())
->all();
}
public static function optionLabel(Photo $photo): string
{
$thumbnail = $photo->url('sm');
$image = $thumbnail !== ''
? '<img src="'.e($thumbnail).'" alt="" loading="lazy" style="width:4.5rem;height:3rem;object-fit:cover;border-radius:.25rem;flex-shrink:0">'
: '<span style="width:4.5rem;height:3rem;border-radius:.25rem;background:#e5e7eb;flex-shrink:0"></span>';
$hidden = $photo->is_published ? '' : ' <em style="opacity:.6">(ausgeblendet)</em>';
$seasons = $photo->appearanceNames() === [] ? '' : ' <em style="opacity:.75">· nur '.e(implode(', ', $photo->appearanceNames())).'</em>';
return '<span style="display:flex;align-items:center;gap:.75rem">'.$image.'<span>'.e($photo->alt_text).$seasons.$hidden.'</span></span>';
}
}