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()
->options(function () {
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) {
try {
$url = $item->getUrl();
} catch (\Exception $e) {
// Skip items that can't generate URLs
return [];
}
$fileName = e($item->file_name);
$name = e($item->name ?? '');
$html = "<div class='flex items-center gap-2 w-full'>".
"<img src=\"{$url}\" class=\"rounded\" style=\"max-width:200px;max-height:200px;object-fit:cover;width:auto;height:auto;\" alt=\"{$fileName}\"/>".
"<span class=\"ml-2 truncate\">{$name}{$fileName}</span></div>";
// Smaller image preview for better performance
$html = "<div class='flex items-center gap-3'>" .
"<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];
})->toArray();
})
->searchable()
->preload()
->required(),
])
->action(function (array $data, SpatieMediaLibraryFileUpload $component) {
@ -82,16 +97,15 @@ class EntryForm
// Clear existing featured image
$record->clearMediaCollection('featured-image');
// Download from the full URL and add as new media
$fullUrl = url($mediaItem->getUrl());
$newMedia = $record->addMediaFromUrl($fullUrl)
->usingName($mediaItem->name ?: $mediaItem->file_name)
->usingFileName($mediaItem->file_name)
->toMediaCollection('featured-image', 'public');
// Associate the existing media with this entry instead of copying
$mediaItem->update([
'model_type' => get_class($record),
'model_id' => $record->id,
'collection_name' => 'featured-image'
]);
// Update component state
$component->state([$newMedia->uuid]);
$component->state([$mediaItem->uuid]);
\Filament\Notifications\Notification::make()
->success()
@ -116,24 +130,37 @@ class EntryForm
->label('Select an existing image')
->allowHtml()
->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()
->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) {
try {
$url = $item->getUrl();
} catch (\Exception $e) {
// Skip items that can't generate URLs
return [];
}
$fileName = e($item->file_name);
$name = e($item->name ?? '');
$html = "<div class='flex items-center gap-2 w-full'>".
"<img src=\"{$url}\" class=\"rounded\" style=\"max-width:200px;max-height:200px;object-fit:cover;width:auto;height:auto;\" alt=\"{$fileName}\"/>".
"<span class=\"ml-2 truncate\">{$name}{$fileName}</span></div>";
// Smaller image preview for better performance
$html = "<div class='flex items-center gap-3'>" .
"<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];
})->toArray();
})
->searchable()
->preload()
->required(),
])
->action(function (array $data, RichEditor $component) {