Skip to content

Commit

Permalink
INIT
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvenga committed Apr 8, 2023
1 parent 32bb86d commit 5f2ff0b
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 24 deletions.
26 changes: 3 additions & 23 deletions .gitignore
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
8 changes: 7 additions & 1 deletion README.md
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)
60 changes: 60 additions & 0 deletions composer.json
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
}
}
]
}
81 changes: 81 additions & 0 deletions src/MediaLibrary.php
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 '';
}
}

0 comments on commit 5f2ff0b

Please sign in to comment.