fix: partical fix for featured

imaes are added, seem semi permanent

disapapar in media lib
This commit is contained in:
jon brookes 2026-01-03 14:25:23 +00:00
parent 340b466ade
commit c49249ee20

View file

@ -11,6 +11,7 @@ use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle; use Filament\Forms\Components\Toggle;
use Filament\Schemas\Schema; use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\MediaLibrary\MediaCollections\Models\Media;
@ -67,22 +68,33 @@ class EntryForm
->action(function (array $data, SpatieMediaLibraryFileUpload $component) { ->action(function (array $data, SpatieMediaLibraryFileUpload $component) {
$record = $component->getRecord(); $record = $component->getRecord();
if ($record && $mediaItem = Media::find($data['image_id'])) { if (!$record) {
// Clear any existing featured image \Filament\Notifications\Notification::make()
->warning()
->title('Save the entry first')
->send();
return;
}
if ($mediaItem = Media::find($data['image_id'])) {
// Clear existing featured image
$record->clearMediaCollection('featured-image'); $record->clearMediaCollection('featured-image');
// Get the full path to the media file // Download from the full URL and add as new media
$fullPath = $mediaItem->getPath(); $fullUrl = url($mediaItem->getUrl());
// Add a copy of the media file to the featured-image collection $newMedia = $record->addMediaFromUrl($fullUrl)
$record->addMedia($fullPath)
->usingName($mediaItem->name ?: $mediaItem->file_name) ->usingName($mediaItem->name ?: $mediaItem->file_name)
->usingFileName($mediaItem->file_name) ->usingFileName($mediaItem->file_name)
->preservingOriginal()
->toMediaCollection('featured-image'); ->toMediaCollection('featured-image');
// Refresh the page to show the new featured image // Update component state
$component->getLivewire()->dispatch('$refresh'); $component->state([$newMedia->uuid]);
\Filament\Notifications\Notification::make()
->success()
->title('Featured image set')
->send();
} }
}) })
), ),