Skip to content

Commit

Permalink
Rewrite some format getter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryMoustache committed Sep 29, 2023
1 parent 9f78a1f commit ef97239
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/Filament/AttachmentInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Codedor\MediaLibrary\Facades\Formats;
use Codedor\MediaLibrary\Filament\Actions\Forms\UploadAttachmentAction;
use Codedor\MediaLibrary\Formats\Format;
use Codedor\MediaLibrary\Models\Attachment;
use Codedor\MediaLibrary\Resources\AttachmentResource;
use Filament\Forms\Components\Actions\Action;
Expand Down Expand Up @@ -192,18 +193,18 @@ public function getAllowedFormats(): ?array
{
$formats = $this->evaluate($this->allowedFormats);

// Get the model that we are editing/viewing
if (is_null($formats)) {
// Get the model that we are editing/viewing
try {
$model = $this->getModelInstance();
$formats = Formats::when($model, fn ($formats) => $formats->get($model::class) ?? collect());
} catch (\Throwable $th) {
$formats = Formats::all();
$model = $this->getModelInstance();
if (is_null($model)) {
$formats = Formats::flatten(1)->unique(fn (Format $format) => $format::class);
} else {
$formats = Formats::get($model::class);
}
}

return Collection::wrap($formats)
->map(fn ($format) => is_string($format) ? $format : get_class($format))
->map(fn (string|Format $format) => is_string($format) ? $format : $format::class)
->toArray();
}

Expand Down

0 comments on commit ef97239

Please sign in to comment.