-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,3 @@ | ||
/vendor/ | ||
node_modules/ | ||
npm-debug.log | ||
yarn-error.log | ||
|
||
# Laravel 4 specific | ||
bootstrap/compiled.php | ||
app/storage/ | ||
|
||
# Laravel 5 & Lumen specific | ||
public/storage | ||
public/hot | ||
|
||
# Laravel 5 & Lumen specific with changed public path | ||
public_html/storage | ||
public_html/hot | ||
|
||
storage/*.key | ||
.env | ||
Homestead.yaml | ||
Homestead.json | ||
/.vagrant | ||
.phpunit.result.cache | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
# moonshine-spatie-laravel-medialibrary | ||
# Spatie\MediaLibrary field for [MoonShine Laravel admin panel](https://moonshine.cutcode.dev) | ||
|
||
[![Latest Version on Packagist](https://img.shields.io/packagist/v/visual-ideas/moonshine-spatie-medialibrary.svg?style=flat-square)](https://packagist.org/packages/visual-ideas/laravel-site-settings) | ||
[![Total Downloads](https://img.shields.io/packagist/dt/visual-ideas/moonshine-spatie-medialibrary.svg?style=flat-square)](https://packagist.org/packages/visual-ideas/laravel-site-settings) | ||
|
||
## Documentation | ||
[Read Spatie\MediaLibrary field in Official MoonShine Documentation](https://moonshine.cutcode.dev/section/fields-spatie-medialibrary) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"name": "visual-ideas/moonshine-spatie-medialibrary", | ||
"description": "Spatie\\MediaLibrary field for MoonShine Laravel admin panel", | ||
"keywords": [ | ||
"PHP", | ||
"Gallery", | ||
"Laravel", | ||
"Eloquent", | ||
"Images", | ||
"Upload", | ||
"Media", | ||
"VisualIdeas", | ||
"VI", | ||
"Spatie", | ||
"MoonShine", | ||
"Spatie MediaLibrary", | ||
"Spatie Laravel MediaLibrary" | ||
], | ||
"homepage": "https://github.com/visual-ideas/moonshine-spatie-medialibrary", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Alex", | ||
"email": "alex.visualideas@gmail.com", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0|^8.1|^8.2", | ||
"lee-to/moonshine": "@dev", | ||
"laravel/framework": "^9.0|^10.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"VI\\MoonShine\\Fields\\Spatie\\MediaLibrary\\": "src" | ||
}, | ||
"files": [ | ||
] | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
], | ||
"aliases": { | ||
} | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
|
||
"repositories": [ | ||
{ | ||
"type": "path", | ||
"url": "/usr/local/var/www/visual-ideas/moonshine", | ||
"options": { | ||
"symlink": true | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace VI\MoonShine\Fields\Spatie\MediaLibrary; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Leeto\MoonShine\Fields\Field; | ||
use Leeto\MoonShine\Traits\Fields\CanBeMultiple; | ||
|
||
class MediaLibrary extends Field | ||
{ | ||
use CanBeMultiple; | ||
|
||
public static string $view = 'moonshine::fields.image'; | ||
|
||
public static string $type = 'file'; | ||
|
||
public function save(Model $item): Model | ||
{ | ||
return $item; | ||
} | ||
|
||
public function afterSave(Model $item): void | ||
{ | ||
if ($this->isCanSave() && $this->requestValue()) { | ||
if ($this->isMultiple()) { | ||
$item->clearMediaCollection($this->field()); | ||
|
||
foreach ($this->requestValue() as $file) { | ||
$item->addMedia($file) | ||
->preservingOriginal() | ||
->toMediaCollection($this->field()); | ||
} | ||
} else { | ||
if ($media = $item->getFirstMedia($this->field())) { | ||
$media->delete(); | ||
} | ||
|
||
$item->addMedia($this->requestValue()) | ||
->preservingOriginal() | ||
->toMediaCollection($this->field()); | ||
} | ||
} | ||
} | ||
|
||
public function indexViewValue(Model $item, bool $container = true): string | ||
{ | ||
if ($this->isMultiple()) { | ||
return view('moonshine::ui.image', [ | ||
'values' => $item->getMedia($this->field()) | ||
->map(fn($value) => $value->getUrl()) | ||
->toArray(), | ||
])->render(); | ||
} | ||
|
||
$url = $item->getFirstMediaUrl($this->field()); | ||
|
||
if (empty($url)) { | ||
return ''; | ||
} | ||
|
||
return view('moonshine::ui.image', [ | ||
'value' => $url, | ||
])->render(); | ||
} | ||
|
||
public function formViewValue(Model $item): mixed | ||
{ | ||
if ($this->isMultiple()) { | ||
return $item->getMedia($this->field()) | ||
->map(fn ($value) => $value->getUrl())->toArray(); | ||
} | ||
|
||
return $item->getFirstMediaUrl($this->field()); | ||
} | ||
|
||
|
||
public function path(string $value): string | ||
{ | ||
return ''; | ||
} | ||
} |