share-lt/app/Filament/Resources/Entries/Schemas/EntryForm.php

137 lines
7 KiB
PHP
Raw Normal View History

2026-01-02 13:58:06 +00:00
<?php
namespace App\Filament\Resources\Entries\Schemas;
use Filament\Actions\Action;
2026-01-02 13:58:06 +00:00
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
2026-01-02 13:58:06 +00:00
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
2026-01-02 13:58:06 +00:00
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Schema;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
2026-01-02 13:58:06 +00:00
class EntryForm
{
public static function configure(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('title')
->required()
->live(onBlur: true)
2026-01-02 13:58:06 +00:00
->afterStateUpdated(function ($state, $set): void {
$set('slug', Str::slug((string) $state));
}),
TextInput::make('slug')
->required()
->dehydrated()
->readOnly(),
2026-01-02 13:58:06 +00:00
Textarea::make('description')
->columnSpanFull(),
SpatieMediaLibraryFileUpload::make('featured_image')
->collection('featured-image')
->image()
->imageEditor()
->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');
}
})
),
2026-01-02 13:58:06 +00:00
Toggle::make('is_published')
->required(),
Toggle::make('is_featured')
->required(),
DatePicker::make('published_at'),
RichEditor::make('content')
->columnSpanFull()
->hintAction(
Action::make('picker')
->label('Gallery Picker')
->icon('heroicon-m-photo')
->schema([
Select::make('image_url')
->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()
->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 [$url => $html];
})->toArray();
})
->searchable()
->required(),
])
->action(function (array $data, RichEditor $component) {
// We dispatch the URL to the browser to be inserted into TipTap
$component->getLivewire()->dispatch('insert-editor-content', [
'statePath' => $component->getStatePath(),
'html' => "<img src='{$data['image_url']}' alt=''>",
]);
})
),
2026-01-02 13:58:06 +00:00
]);
}
}