fix: partical images broken

public seems to be holding

still issues with picking images - not saved
This commit is contained in:
jon brookes 2026-01-03 15:12:34 +00:00
parent 6d1d88542e
commit 8e1650653b

View file

@ -50,21 +50,36 @@ class EntryForm
->allowHtml() ->allowHtml()
->options(function () { ->options(function () {
return Media::latest() return Media::latest()
->get() ->limit(30) // Limit to 30 most recent items for performance
->get(['id', 'file_name', 'name', 'uuid', 'collection_name', 'model_type', 'model_id', 'disk'])
->filter(function (Media $item) {
// Only include media items that have a valid disk
return $item->disk !== null;
})
->mapWithKeys(function (Media $item) { ->mapWithKeys(function (Media $item) {
try {
$url = $item->getUrl(); $url = $item->getUrl();
} catch (\Exception $e) {
// Skip items that can't generate URLs
return [];
}
$fileName = e($item->file_name); $fileName = e($item->file_name);
$name = e($item->name ?? ''); $name = e($item->name ?? '');
$html = "<div class='flex items-center gap-2 w-full'>". // Smaller image preview for better performance
"<img src=\"{$url}\" class=\"rounded\" style=\"max-width:200px;max-height:200px;object-fit:cover;width:auto;height:auto;\" alt=\"{$fileName}\"/>". $html = "<div class='flex items-center gap-3'>" .
"<span class=\"ml-2 truncate\">{$name}{$fileName}</span></div>"; "<img src='{$url}' class='rounded' style='width:60px;height:60px;object-fit:cover;' alt='{$fileName}' loading='lazy' />" .
"<div class='flex flex-col'>" .
"<span class='font-medium text-sm'>{$name}</span>" .
"<span class='text-xs text-gray-500'>{$fileName}</span>" .
"</div></div>";
return [$item->id => $html]; return [$item->id => $html];
})->toArray(); })->toArray();
}) })
->searchable() ->searchable()
->preload()
->required(), ->required(),
]) ])
->action(function (array $data, SpatieMediaLibraryFileUpload $component) { ->action(function (array $data, SpatieMediaLibraryFileUpload $component) {
@ -82,16 +97,15 @@ class EntryForm
// Clear existing featured image // Clear existing featured image
$record->clearMediaCollection('featured-image'); $record->clearMediaCollection('featured-image');
// Download from the full URL and add as new media // Associate the existing media with this entry instead of copying
$fullUrl = url($mediaItem->getUrl()); $mediaItem->update([
'model_type' => get_class($record),
$newMedia = $record->addMediaFromUrl($fullUrl) 'model_id' => $record->id,
->usingName($mediaItem->name ?: $mediaItem->file_name) 'collection_name' => 'featured-image'
->usingFileName($mediaItem->file_name) ]);
->toMediaCollection('featured-image', 'public');
// Update component state // Update component state
$component->state([$newMedia->uuid]); $component->state([$mediaItem->uuid]);
\Filament\Notifications\Notification::make() \Filament\Notifications\Notification::make()
->success() ->success()
@ -116,24 +130,37 @@ class EntryForm
->label('Select an existing image') ->label('Select an existing image')
->allowHtml() ->allowHtml()
->options(function () { ->options(function () {
// We must 'get' the collection first so we can call getUrl()
// because 'url' is not a column in the Spatie database table.
return Media::latest() return Media::latest()
->get() ->limit(30) // Limit to 30 most recent items for performance
->get(['id', 'file_name', 'name', 'uuid', 'collection_name', 'model_type', 'model_id', 'disk'])
->filter(function (Media $item) {
// Only include media items that have a valid disk
return $item->disk !== null;
})
->mapWithKeys(function (Media $item) { ->mapWithKeys(function (Media $item) {
try {
$url = $item->getUrl(); $url = $item->getUrl();
} catch (\Exception $e) {
// Skip items that can't generate URLs
return [];
}
$fileName = e($item->file_name); $fileName = e($item->file_name);
$name = e($item->name ?? ''); $name = e($item->name ?? '');
$html = "<div class='flex items-center gap-2 w-full'>". // Smaller image preview for better performance
"<img src=\"{$url}\" class=\"rounded\" style=\"max-width:200px;max-height:200px;object-fit:cover;width:auto;height:auto;\" alt=\"{$fileName}\"/>". $html = "<div class='flex items-center gap-3'>" .
"<span class=\"ml-2 truncate\">{$name}{$fileName}</span></div>"; "<img src='{$url}' class='rounded' style='width:60px;height:60px;object-fit:cover;' alt='{$fileName}' loading='lazy' />" .
"<div class='flex flex-col'>" .
"<span class='font-medium text-sm'>{$name}</span>" .
"<span class='text-xs text-gray-500'>{$fileName}</span>" .
"</div></div>";
return [$url => $html]; return [$url => $html];
})->toArray(); })->toArray();
}) })
->searchable() ->searchable()
->preload()
->required(), ->required(),
]) ])
->action(function (array $data, RichEditor $component) { ->action(function (array $data, RichEditor $component) {