Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from ragingdave/patch-1
Browse files Browse the repository at this point in the history
Make customProperties work as expected
  • Loading branch information
jameslkingsley authored Nov 27, 2018
2 parents 44a6d08 + 1f2f9c2 commit f34dba5
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 177 deletions.
6 changes: 0 additions & 6 deletions resources/js/components/File/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
}
},
Expand Down
6 changes: 0 additions & 6 deletions resources/js/components/Image/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
}
},
Expand Down
102 changes: 2 additions & 100 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,14 @@

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.
*
* @var string
*/
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'];
}
76 changes: 11 additions & 65 deletions src/Fields/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -84,20 +29,19 @@ 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)) {
$media->preview_url = url($media->getUrl($conversion));
} else {
$media->preview_url = url($media->getUrl());
}

return $media;
}

return null;
return $media;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
130 changes: 130 additions & 0 deletions src/Fields/MediaField.php
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);
}
}

0 comments on commit f34dba5

Please sign in to comment.