diff --git a/resources/js/components/File/FormField.vue b/resources/js/components/File/FormField.vue index 18062c4..f3dade0 100644 --- a/resources/js/components/File/FormField.vue +++ b/resources/js/components/File/FormField.vue @@ -93,12 +93,6 @@ mounted() { this.field.fill = formData => { formData.append(this.field.attribute, this.file, this.fileName) - - for (let attribute in this.field) { - if (attribute.substr(0, 3) === 'ml_') { - formData.append(attribute, this.field[attribute]) - } - } } }, diff --git a/resources/js/components/Image/FormField.vue b/resources/js/components/Image/FormField.vue index ca2b70a..7fb835e 100644 --- a/resources/js/components/Image/FormField.vue +++ b/resources/js/components/Image/FormField.vue @@ -114,12 +114,6 @@ mounted() { this.field.fill = formData => { formData.append(this.field.attribute, this.file, this.fileName) - - for (let attribute in this.field) { - if (attribute.substr(0, 3) === 'ml_') { - formData.append(attribute, this.field[attribute]) - } - } } }, diff --git a/src/Fields/File.php b/src/Fields/File.php index d867046..9413616 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -2,17 +2,8 @@ namespace Kingsley\NovaMediaLibrary\Fields; -use Illuminate\Support\Str; -use Illuminate\Http\Request; -use Laravel\Nova\Fields\Field; -use Laravel\Nova\Fields\Deletable; -use Laravel\Nova\Http\Requests\NovaRequest; -use Laravel\Nova\Contracts\Deletable as DeletableContract; - -class File extends Field implements DeletableContract +class File extends MediaField { - use Deletable; - /** * The field's component. * @@ -20,94 +11,5 @@ class File extends Field implements DeletableContract */ public $component = 'nova-media-library-file-field'; - /** - * The media collection. - * - * @var string - */ - public $mediaCollection; - - /** - * 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 => 'file' - ]); - - $query = $model->addMedia($request[$requestAttribute]); - - foreach ($request->all() as $key => $value) { - if (starts_with($key, 'ml_')) { - $method = substr($key, 3); - $arguments = is_array($value) ? $value : [$value]; - $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) - { - $media = $resource->getFirstMedia($this->mediaCollection); - - return $media ?? null; - } - - /** - * Dynamically set a media-library setting on the field. - * - * @return $this - */ - public function __call($method, $arguments) - { - return $this->withMeta(['ml_' . $method => $arguments]); - } - - /** - * 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); - } + protected $validationRules = ['file']; } diff --git a/src/Fields/Image.php b/src/Fields/Image.php index b36b542..da66a9f 100644 --- a/src/Fields/Image.php +++ b/src/Fields/Image.php @@ -2,17 +2,10 @@ namespace Kingsley\NovaMediaLibrary\Fields; -use Illuminate\Support\Str; -use Illuminate\Http\Request; -use Laravel\Nova\Fields\Field; -use Laravel\Nova\Fields\Deletable; use Laravel\Nova\Http\Requests\NovaRequest; -use Laravel\Nova\Contracts\Deletable as DeletableContract; -class Image extends Field implements DeletableContract +class Image extends MediaField { - use Deletable; - /** * The field's component. * @@ -21,59 +14,11 @@ class Image extends Field implements DeletableContract public $component = 'nova-media-library-image-field'; /** - * The media collection. - * - * @var string - */ - public $mediaCollection; - - /** - * 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. + * The validation rules. * - * @param \Laravel\Nova\Http\Requests\NovaRequest $request - * @param string $requestAttribute - * @param object $model - * @param string $attribute - * @return void + * @var array */ - protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute) - { - if (! $request[$requestAttribute]) { - return; - } - - $request->validate([ - $requestAttribute => 'image' - ]); - - $query = $model->addMedia($request[$requestAttribute]); - - foreach ($request->all() as $key => $value) { - if (starts_with($key, 'ml_')) { - $method = substr($key, 3); - $arguments = is_array($value) ? $value : [$value]; - $query->$method(...$arguments); - } - } - - $query->toMediaCollection($this->mediaCollection); - } + protected $validationRules = ['image']; /** * Resolve the given attribute from the given resource. @@ -84,8 +29,9 @@ protected function fillAttributeFromRequest(NovaRequest $request, $requestAttrib */ protected function resolveAttribute($resource, $attribute) { - $conversion = $this->meta()['usingConversion'] ?? ''; - $media = $resource->getFirstMedia($this->mediaCollection); + $media = parent::resolveAttribute($resource, $attribute); + + $conversion = $this->meta()['usingConversion'] ?? []; if ($media) { if ($media->hasGeneratedConversion($conversion)) { @@ -93,11 +39,9 @@ protected function resolveAttribute($resource, $attribute) } else { $media->preview_url = url($media->getUrl()); } - - return $media; } - return null; + return $media; } /** @@ -153,7 +97,9 @@ public function usingConversion(string $name) */ public function __call($method, $arguments) { - return $this->withMeta(['ml_' . $method => $arguments]); + $this->mediaLibraryMethods[$method] = $arguments; + + return $this; } /** diff --git a/src/Fields/MediaField.php b/src/Fields/MediaField.php new file mode 100644 index 0000000..6cc1c3f --- /dev/null +++ b/src/Fields/MediaField.php @@ -0,0 +1,130 @@ +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); + } +}