feat: enhance featured image upload
with gallery selection and preview
This commit is contained in:
parent
9f01d44c9d
commit
340b466ade
1 changed files with 50 additions and 1 deletions
|
|
@ -36,7 +36,56 @@ class EntryForm
|
|||
->collection('featured-image')
|
||||
->image()
|
||||
->imageEditor()
|
||||
->columnSpanFull(),
|
||||
->columnSpanFull()
|
||||
->hintAction(
|
||||
Action::make('featured_picker')
|
||||
->label('Pick from Gallery')
|
||||
->icon('heroicon-m-photo')
|
||||
->schema([
|
||||
Select::make('image_id')
|
||||
->label('Select an existing image')
|
||||
->allowHtml()
|
||||
->options(function () {
|
||||
return Media::latest()
|
||||
->get()
|
||||
->mapWithKeys(function (Media $item) {
|
||||
$url = $item->getUrl();
|
||||
|
||||
$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>";
|
||||
|
||||
return [$item->id => $html];
|
||||
})->toArray();
|
||||
})
|
||||
->searchable()
|
||||
->required(),
|
||||
])
|
||||
->action(function (array $data, SpatieMediaLibraryFileUpload $component) {
|
||||
$record = $component->getRecord();
|
||||
|
||||
if ($record && $mediaItem = Media::find($data['image_id'])) {
|
||||
// Clear any existing featured image
|
||||
$record->clearMediaCollection('featured-image');
|
||||
|
||||
// Get the full path to the media file
|
||||
$fullPath = $mediaItem->getPath();
|
||||
|
||||
// Add a copy of the media file to the featured-image collection
|
||||
$record->addMedia($fullPath)
|
||||
->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');
|
||||
}
|
||||
})
|
||||
),
|
||||
Toggle::make('is_published')
|
||||
->required(),
|
||||
Toggle::make('is_featured')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue