From 340b466aded2ee95f7fa732e02c937987b1d44a0 Mon Sep 17 00:00:00 2001 From: jon brookes Date: Sat, 3 Jan 2026 13:35:38 +0000 Subject: [PATCH] feat: enhance featured image upload with gallery selection and preview --- .../Resources/Entries/Schemas/EntryForm.php | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/app/Filament/Resources/Entries/Schemas/EntryForm.php b/app/Filament/Resources/Entries/Schemas/EntryForm.php index 41c94e5..a0613d3 100644 --- a/app/Filament/Resources/Entries/Schemas/EntryForm.php +++ b/app/Filament/Resources/Entries/Schemas/EntryForm.php @@ -36,7 +36,56 @@ class EntryForm ->collection('featured-image') ->image() ->imageEditor() - ->columnSpanFull(), + ->columnSpanFull() + ->hintAction( + Action::make('featured_picker') + ->label('Pick from Gallery') + ->icon('heroicon-m-photo') + ->schema([ + Select::make('image_id') + ->label('Select an existing image') + ->allowHtml() + ->options(function () { + return Media::latest() + ->get() + ->mapWithKeys(function (Media $item) { + $url = $item->getUrl(); + + $fileName = e($item->file_name); + $name = e($item->name ?? ''); + + $html = "
". + "\"{$fileName}\"/". + "{$name} — {$fileName}
"; + + return [$item->id => $html]; + })->toArray(); + }) + ->searchable() + ->required(), + ]) + ->action(function (array $data, SpatieMediaLibraryFileUpload $component) { + $record = $component->getRecord(); + + if ($record && $mediaItem = Media::find($data['image_id'])) { + // Clear any existing featured image + $record->clearMediaCollection('featured-image'); + + // Get the full path to the media file + $fullPath = $mediaItem->getPath(); + + // Add a copy of the media file to the featured-image collection + $record->addMedia($fullPath) + ->usingName($mediaItem->name ?: $mediaItem->file_name) + ->usingFileName($mediaItem->file_name) + ->preservingOriginal() + ->toMediaCollection('featured-image'); + + // Refresh the page to show the new featured image + $component->getLivewire()->dispatch('$refresh'); + } + }) + ), Toggle::make('is_published') ->required(), Toggle::make('is_featured')