Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: moonshine 3+ сompatibility #14

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

## Compatibility

| MoonShine | Moonshine Spatie Medialibrary | Currently supported |
|:---------------------:|:-----------------------------:|:-------------------:|
| \>= v1.52 and < v2.0 | <= v1.2.0 | no |
| >= v2.0 | >= v2.0.1 | yes |
| MoonShine | Moonshine Spatie Medialibrary | Currently supported |
|:--------------------:|:-----------------------------:|:-------------------:|
| \>= v1.52 and < v2.0 | <= v1.2.0 | no |
| >= v2.0 | >= v2.0.1 | yes |
| >= v3.0 | >= v3.0.0 | yes |


## Installation
Expand Down
57 changes: 49 additions & 8 deletions src/Fields/MediaLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use MoonShine\Fields\Image;
use MoonShine\Traits\Fields\FileDeletable;
use MoonShine\Contracts\UI\FieldContract;
use MoonShine\Support\DTOs\FileItem;
use MoonShine\UI\Fields\Image;
use MoonShine\UI\Traits\Fields\FileDeletable;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

Expand All @@ -17,7 +19,7 @@ class MediaLibrary extends Image

protected function prepareFill(array $raw = [], mixed $casted = null): mixed
{
$value = $casted->getMedia($this->column());
$value = $casted->getOriginal()->getMedia($this->column);

if (!$this->isMultiple()) {
$value = $value->first();
Expand Down Expand Up @@ -46,11 +48,11 @@ protected function resolveOnApply(): ?Closure

protected function resolveAfterApply(mixed $data): mixed
{
$oldValues = request()->collect($this->hiddenOldValuesKey())->map(
$oldValues = request()->collect($this->getHiddenRemainingValuesKey())->map(
fn($model) => Media::make(json_decode($model, true))
);

$requestValue = $this->requestValue();
$requestValue = $this->getRequestValue();

$recentlyCreated = collect();
if ($requestValue !== false) {
Expand All @@ -74,15 +76,15 @@ protected function resolveAfterApply(mixed $data): mixed
protected function resolveAfterDestroy(mixed $data): mixed
{
$data
->getMedia($this->column())
->getMedia($this->column)
->each(fn(Media $media) => $media->delete());

return $data;
}

private function removeOldMedia(HasMedia $item, Collection $recentlyCreated, Collection $oldValues): void
{
foreach ($item->getMedia($this->column()) as $media) {
foreach ($item->getMedia($this->column) as $media) {
if (
!$recentlyCreated->contains('id', $media->getKey())
&& !$oldValues->contains('id', $media->getKey())
Expand All @@ -96,11 +98,50 @@ private function addMedia(HasMedia $item, UploadedFile $file): Media
{
return $item->addMedia($file)
->preservingOriginal()
->toMediaCollection($this->column());
->toMediaCollection($this->column);
}

private function orderMedia(Collection $recentlyCreated): void
{
Media::setNewOrder($recentlyCreated->pluck('id')->toArray());
}

protected function getFiles(): Collection
{
return collect($this->getFullPathValues())
->mapWithKeys(fn (string $path, int $index): array => [
$index => new FileItem(
fullPath: $path,
rawValue: data_get($this->toValue(), $index, $this->toValue()),
name: (string) \call_user_func($this->resolveNames(), $path, $index, $this),
attributes: \call_user_func($this->resolveItemAttributes(), $path, $index, $this),
),
]);
}

public function removeExcludedFiles(): void
{
$values = collect([
$this->toValue(withDefault: false)
]);

$values->diff([$this->getValue()])->each(fn (string $file) => $this->deleteFile($file));
}

public function getRequestValue(int|string|null $index = null): mixed
{
return $this->prepareRequestValue(
$this->getCore()->getRequest()->getFile(
$this->getRequestNameDot($index),
) ?? false
);
}

public function apply(Closure $default, mixed $data): mixed
{
$item = parent::apply($default, $data);
unset($item->{$this->column});

return $item;
}
}