diff --git a/composer.json b/composer.json index 919382d..b98b2e8 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "php": "^8.2", "blade-ui-kit/blade-icons": "^1.5", "codedor/filament-translatable-tabs": "^0.1", + "codedor/filament-resource-picker": "dev-master as 0.1", "filament/filament": "^3.0", "illuminate/contracts": "^10.0", "livewire/livewire": "^3.0", diff --git a/resources/views/components/picture.blade.php b/resources/views/components/picture.blade.php index 5b30e87..99ff5b5 100644 --- a/resources/views/components/picture.blade.php +++ b/resources/views/components/picture.blade.php @@ -6,7 +6,7 @@ :class="$class" :alt="$alt" /> -@elseif (! $formats && ! $formats) +@elseif ($format && ! $formats) {{ $alt }} $lazyload, ]) - src="{{ $image->getFormatOrOriginal('thumbnail') }}" + src="{{ $image->getFormatOrOriginal($format) }}" > @elseif ($image) diff --git a/resources/views/components/resource-picker/item.blade.php b/resources/views/components/resource-picker/item.blade.php new file mode 100644 index 0000000..5e32ab4 --- /dev/null +++ b/resources/views/components/resource-picker/item.blade.php @@ -0,0 +1,12 @@ + diff --git a/resources/views/filament/attachment-input.blade.php b/resources/views/filament/attachment-input.blade.php index 8ec506a..baf3f79 100644 --- a/resources/views/filament/attachment-input.blade.php +++ b/resources/views/filament/attachment-input.blade.php @@ -1,4 +1,12 @@ - + @php $attachments = $getAttachments(); @endphp diff --git a/src/Filament/AttachmentInput.php b/src/Filament/AttachmentInput.php index 2db6965..6e0d3d2 100644 --- a/src/Filament/AttachmentInput.php +++ b/src/Filament/AttachmentInput.php @@ -53,6 +53,18 @@ protected function setUp(): void $component->getRelationship()->sync($state->toArray()); }); + $this->dehydrateStateUsing(function (self $component, $state) { + if ($component->isMultiple()) { + return; + } + + if (is_string($state)) { + return $state; + } + + return $state[0] ?? null; + }); + $this->registerActions([ Action::make('remove-attachment') ->icon('heroicon-o-x-circle') @@ -101,7 +113,22 @@ protected function setUp(): void Action::make('attachment-picker') ->label(__('filament-media-library::picker.select existing media')) - ->color('gray'), + ->modalHeading(__('filament-media-library::picker.select existing media')) + ->color('gray') + ->modalSubmitAction(false) + ->modalCancelAction(false) + ->modalContent(function (self $component) { + return view('filament-resource-picker::picker', [ + 'resourceClass' => AttachmentResource::class, + 'displayType' => 'filament-media-library::resource-picker.item', + 'statePath' => $component->getStatePath(), + 'state' => $component->getState() ?? [], + 'keyField' => 'id', + 'labelField' => 'id', + 'isMultiple' => $component->isMultiple(), + 'isGrid' => true, + ]); + }), ]); } diff --git a/src/Resources/AttachmentResource.php b/src/Resources/AttachmentResource.php index cbe323a..689f218 100644 --- a/src/Resources/AttachmentResource.php +++ b/src/Resources/AttachmentResource.php @@ -22,6 +22,7 @@ use Filament\Tables\Columns\TextColumn; use Filament\Tables\Filters; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; use Livewire\Component; @@ -226,4 +227,13 @@ public static function getPages(): array 'edit' => Pages\EditAttachment::route('/{record}/edit'), ]; } + + public static function resourcePickerQuery(Builder $query, string $search = null): Builder + { + return $query + ->when( + $search, + fn () => $query->where('name', 'like', '%' . $search . '%') + ); + } }