added fixes: warning

some files likely wont be needed and wer added by ai to fix things that were no longer needed !!!
This commit is contained in:
jon brookes 2026-01-03 17:26:18 +00:00
parent 8e1650653b
commit 93c977d1f5
5 changed files with 288 additions and 38 deletions

View file

@ -40,6 +40,7 @@ class EntryForm
->disk('public')
->visibility('public')
->columnSpanFull()
->dehydrated(false)
->hintAction(
Action::make('featured_picker')
->label('Pick from Gallery')
@ -49,40 +50,36 @@ class EntryForm
->label('Select an existing image')
->allowHtml()
->options(function () {
return Media::latest()
->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;
})
return Media::where('model_type', 'temp')
->where('model_id', 0)
->where('disk', 'public')
->latest()
->limit(30)
->get(['id', 'file_name', 'name', 'disk'])
->mapWithKeys(function (Media $item) {
try {
$url = $item->getUrl();
$fileName = e($item->file_name);
$name = e($item->name ?? '');
$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];
} catch (\Exception $e) {
// Skip items that can't generate URLs
return [];
}
$fileName = e($item->file_name);
$name = e($item->name ?? '');
// 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) {
->action(function (array $data, SpatieMediaLibraryFileUpload $component): void {
$record = $component->getRecord();
if (!$record) {
@ -92,25 +89,56 @@ class EntryForm
->send();
return;
}
if ($mediaItem = Media::find($data['image_id'])) {
// Clear existing featured image
$record->clearMediaCollection('featured-image');
// 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([$mediaItem->uuid]);
if (!$data['image_id']) {
return;
}
$sourceMedia = Media::find($data['image_id']);
if (!$sourceMedia || !file_exists($sourceMedia->getPath())) {
\Filament\Notifications\Notification::make()
->danger()
->title('Image file not found')
->send();
return;
}
$sourceFile = $sourceMedia->getPath();
$tempCopy = sys_get_temp_dir() . '/' . uniqid() . '_' . $sourceMedia->file_name;
copy($sourceFile, $tempCopy);
try {
// Verify record has ID
if (!$record->id) {
\Filament\Notifications\Notification::make()
->danger()
->title('Entry must be saved first')
->send();
return;
}
// Add the copy to the entry's featured-image collection
$newMedia = $record->addMedia($tempCopy)
->usingName($sourceMedia->name ?: pathinfo($sourceMedia->file_name, PATHINFO_FILENAME))
->usingFileName($sourceMedia->file_name)
->toMediaCollection('featured-image', 'public');
// Dispatch event for app.js to handle
$component->getLivewire()->dispatch('featured-image-added', ['mediaId' => $newMedia->id]);
\Filament\Notifications\Notification::make()
->success()
->title('Featured image set')
->title('Image added to featured image')
->send();
} catch (\Exception $e) {
\Filament\Notifications\Notification::make()
->danger()
->title('Error: ' . $e->getMessage())
->send();
} finally {
if (file_exists($tempCopy)) {
unlink($tempCopy);
}
}
})
),