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\Toggle;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
@ -67,22 +68,33 @@ class EntryForm
->action(function (array $data, SpatieMediaLibraryFileUpload $component) {
$record = $component->getRecord();
if ($record && $mediaItem = Media::find($data['image_id'])) {
// Clear any existing featured image
if (!$record) {
\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');
// Get the full path to the media file
$fullPath = $mediaItem->getPath();
// Download from the full URL and add as new media
$fullUrl = url($mediaItem->getUrl());
// Add a copy of the media file to the featured-image collection
$record->addMedia($fullPath)
$newMedia = $record->addMediaFromUrl($fullUrl)
->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');
// Update component state
$component->state([$newMedia->uuid]);
\Filament\Notifications\Notification::make()
->success()
->title('Featured image set')
->send();
}
})
),