This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
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.
Merge pull request #19 from ragingdave/patch-1
Make customProperties work as expected
- Loading branch information
Showing
5 changed files
with
143 additions
and
177 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
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
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
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
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,130 @@ | ||
<?php | ||
|
||
namespace Kingsley\NovaMediaLibrary\Fields; | ||
|
||
use Laravel\Nova\Fields\Field; | ||
use Laravel\Nova\Fields\Deletable; | ||
use Laravel\Nova\Http\Requests\NovaRequest; | ||
use Laravel\Nova\Contracts\Deletable as DeletableContract; | ||
|
||
abstract class MediaField extends Field implements DeletableContract | ||
{ | ||
use Deletable; | ||
|
||
/** | ||
* The media collection. | ||
* | ||
* @var string | ||
*/ | ||
public $mediaCollection; | ||
|
||
/** | ||
* The media collection methods | ||
* to apply to this field. | ||
* | ||
* @var array | ||
*/ | ||
protected $mediaLibraryMethods = []; | ||
|
||
/** | ||
* The validation rules. | ||
* | ||
* @var array | ||
*/ | ||
protected $validationRules = []; | ||
|
||
/** | ||
* Create a new field. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(string $name, string $collection = 'default') | ||
{ | ||
parent::__construct($name); | ||
|
||
$this->mediaCollection = $collection; | ||
|
||
$this->delete(function () { | ||
// | ||
}); | ||
} | ||
|
||
/** | ||
* Hydrate the given attribute on the model based on the incoming request. | ||
* | ||
* @param \Laravel\Nova\Http\Requests\NovaRequest $request | ||
* @param string $requestAttribute | ||
* @param object $model | ||
* @param string $attribute | ||
* @return void | ||
*/ | ||
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute) | ||
{ | ||
if (! $request[$requestAttribute]) { | ||
return; | ||
} | ||
|
||
$request->validate([ | ||
$requestAttribute => $this->validationRules | ||
]); | ||
|
||
$query = $model->addMedia($request[$requestAttribute]); | ||
|
||
foreach ($this->mediaLibraryMethods as $method => $arguments) { | ||
$query->$method(...$arguments); | ||
} | ||
|
||
$query->toMediaCollection($this->mediaCollection); | ||
} | ||
|
||
/** | ||
* Resolve the given attribute from the given resource. | ||
* | ||
* @param mixed $resource | ||
* @param string $attribute | ||
* @return mixed | ||
*/ | ||
protected function resolveAttribute($resource, $attribute) | ||
{ | ||
$customProperties = $this->mediaLibraryMethods['withCustomProperties'] ?? ''; | ||
$media = $resource->getMedia($this->mediaCollection); | ||
|
||
if (!empty($customProperties)) { | ||
$customProperties = array_first($customProperties) ?: []; | ||
$media = $media->first(function ($item) use ($customProperties) { | ||
foreach ($customProperties as $property => $value) { | ||
$valid = ($valid ?? true) && $item->getCustomProperty($property) == $value; | ||
} | ||
return $valid ?? false; | ||
}); | ||
} else { | ||
$media = $media->first(); | ||
} | ||
|
||
return $media ?? null; | ||
} | ||
|
||
/** | ||
* Dynamically set a media-library setting on the field. | ||
* | ||
* @return $this | ||
*/ | ||
public function __call($method, $arguments) | ||
{ | ||
$this->mediaLibraryMethods[$method] = $arguments; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get additional meta information to merge with the element payload. | ||
* | ||
* @return array | ||
*/ | ||
public function meta() | ||
{ | ||
return array_merge([ | ||
'deletable' => isset($this->deleteCallback) && $this->deletable | ||
], $this->meta); | ||
} | ||
} |