Skip to content

Commit

Permalink
- added readme & formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ebess committed Aug 29, 2019
1 parent de08ec4 commit a792a30
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 107 deletions.
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ images and order them by drag and drop.
* [Names of uploaded images](#names-of-uploaded-images)
* [Image cropping](#image-cropping)
* [Custom properties](#custom-properties)
* [Custom headers](#custom-headers)
* [Media Field (Video)](#media-field-video)

## Examples
Expand Down Expand Up @@ -186,6 +187,15 @@ Files::make('Multiple files', 'multiple_files')
]);
```

## Custom headers

```php
Images::make('Gallery')
->customHeaders([
'header-name' => 'header-value',
]);
```

## Media Field (Video)

In order to handle videos with thumbnails you need to use the `Media` field instead of `Images`. This way you are able to upload videos as well.
Expand Down
54 changes: 27 additions & 27 deletions src/Fields/HandlesConversionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@
*/
trait HandlesConversionsTrait
{
public function conversionOnIndexView(string $conversionOnIndexView): self
{
return $this->withMeta(compact('conversionOnIndexView'));
}
public function conversionOnIndexView(string $conversionOnIndexView): self
{
return $this->withMeta(compact('conversionOnIndexView'));
}

public function conversionOnDetailView(string $conversionOnDetailView): self
{
return $this->withMeta(compact('conversionOnDetailView'));
}
public function conversionOnDetailView(string $conversionOnDetailView): self
{
return $this->withMeta(compact('conversionOnDetailView'));
}

public function conversionOnForm(string $conversionOnForm): self
{
return $this->withMeta(compact('conversionOnForm'));
}
public function conversionOnForm(string $conversionOnForm): self
{
return $this->withMeta(compact('conversionOnForm'));
}

public function conversionOnPreview(string $conversionOnPreview): self
{
return $this->withMeta(compact('conversionOnPreview'));
}
public function conversionOnPreview(string $conversionOnPreview): self
{
return $this->withMeta(compact('conversionOnPreview'));
}

public function getConversionUrls(\Spatie\MediaLibrary\Models\Media $media): array
{
return [
// original needed several purposes like cropping
'__original__' => $media->getFullUrl(),
'indexView' => $media->getFullUrl($this->meta['conversionOnIndexView'] ?? ''),
'detailView' => $media->getFullUrl($this->meta['conversionOnDetailView'] ?? ''),
'form' => $media->getFullUrl($this->meta['conversionOnForm'] ?? ''),
'preview' => $media->getFullUrl($this->meta['conversionOnPreview'] ?? ''),
];
}
public function getConversionUrls(\Spatie\MediaLibrary\Models\Media $media): array
{
return [
// original needed several purposes like cropping
'__original__' => $media->getFullUrl(),
'indexView' => $media->getFullUrl($this->meta['conversionOnIndexView'] ?? ''),
'detailView' => $media->getFullUrl($this->meta['conversionOnDetailView'] ?? ''),
'form' => $media->getFullUrl($this->meta['conversionOnForm'] ?? ''),
'preview' => $media->getFullUrl($this->meta['conversionOnPreview'] ?? ''),
];
}
}
6 changes: 3 additions & 3 deletions src/Fields/HandlesCustomPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ private function fillMediaCustomPropertiesFromRequest(NovaRequest $request, $med

/** @var Field $field */
foreach ($this->customPropertiesFields as $field) {
$targetAttribute = "custom_properties->{$field->attribute}";
$requestAttribute = "__media-custom-properties__.{$collection}.{$index}.{$field->attribute}";
$targetAttribute = "custom_properties->{$field->attribute}";
$requestAttribute = "__media-custom-properties__.{$collection}.{$index}.{$field->attribute}";

$field->fillInto($request, $media, $targetAttribute, $requestAttribute);
$field->fillInto($request, $media, $targetAttribute, $requestAttribute);
}

$media->save();
Expand Down
18 changes: 9 additions & 9 deletions src/Fields/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function singleImageRules($singleImageRules): self
return $this;
}

public function croppable(bool $croppable = true): self
{
return $this->withMeta(compact('croppable'));
}

public function croppingConfigs(array $configs): self
{
return $this->withMeta(['croppingConfigs' => $configs]);
}
public function croppable(bool $croppable = true): self
{
return $this->withMeta(compact('croppable'));
}

public function croppingConfigs(array $configs): self
{
return $this->withMeta(['croppingConfigs' => $configs]);
}
}
136 changes: 68 additions & 68 deletions src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@ class Media extends Field

protected $collectionMediaRules = [];
protected $singleMediaRules = [];

protected $customHeaders = [];

protected $defaultValidatorRules = [];

public $meta = ['type' => 'media'];

public function serializeMediaUsing(callable $serializeMediaUsing): self
public function serializeMediaUsing(callable $serializeMediaUsing): self
{
$this->serializeMediaCallback = $serializeMediaUsing;

return $this;
}

public function fullSize(): self
public function fullSize(): self
{
return $this->withMeta(['fullSize' => true]);
}

public function rules($rules): self
{
$this->collectionMediaRules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;
public function rules($rules): self
{
$this->collectionMediaRules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;

return $this;
}
return $this;
}

public function singleMediaRules($rules): self
{
$this->singleMediaRules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;

return $this;
}

public function customHeaders(array $headers): self
{
$this->customHeaders = $headers;
return $this;
$this->customHeaders = $headers;

return $this;
}

/**
Expand Down Expand Up @@ -125,7 +125,7 @@ protected function fillAttributeFromRequest(NovaRequest $request, $requestAttrib
});

Validator::make([$requestAttribute => $data], [$requestAttribute => $this->collectionMediaRules])
->validate();
->validate();

return function () use ($request, $data, $attribute, $model) {
$this->handleMedia($request, $model, $attribute, $data);
Expand Down Expand Up @@ -159,10 +159,10 @@ private function addNewMedia(NovaRequest $request, $data, HasMedia $model, strin
if($this->responsive) {
$media->withResponsiveImages();
}
if(!empty($this->customHeaders)) {
$media->addCustomHeaders($this->customHeaders);
}

if(!empty($this->customHeaders)) {
$media->addCustomHeaders($this->customHeaders);
}

if (is_callable($this->setFileNameCallback)) {
$media->setFileName(
Expand Down Expand Up @@ -209,30 +209,30 @@ private function removeDeletedMedia($data, Collection $medias): Collection
*/
public function resolve($resource, $attribute = null)
{
$collectionName = $attribute ?? $this->attribute;
$collectionName = $attribute ?? $this->attribute;

$this->value = $resource->getMedia($collectionName)
$this->value = $resource->getMedia($collectionName)
->map(function (\Spatie\MediaLibrary\Models\Media $media) {
return array_merge($this->serializeMedia($media), ['__media_urls__' => $this->getConversionUrls($media)]);
});

if ($collectionName) {
$this->checkCollectionIsMultiple($resource, $collectionName);
}
if ($collectionName) {
$this->checkCollectionIsMultiple($resource, $collectionName);
}
}

/**
* @param HasMedia|HasMediaTrait $resource
*/
protected function checkCollectionIsMultiple(HasMedia $resource, string $collectionName)
{
$resource->registerMediaCollections();
$isSingle = collect($resource->mediaCollections)
->where('name', $collectionName)
->first()
->singleFile ?? false;

$this->withMeta(['multiple' => !$isSingle]);
/**
* @param HasMedia|HasMediaTrait $resource
*/
protected function checkCollectionIsMultiple(HasMedia $resource, string $collectionName)
{
$resource->registerMediaCollections();
$isSingle = collect($resource->mediaCollections)
->where('name', $collectionName)
->first()
->singleFile ?? false;

$this->withMeta(['multiple' => !$isSingle]);
}

public function serializeMedia(\Spatie\MediaLibrary\Models\Media $media): array
Expand All @@ -244,38 +244,38 @@ public function serializeMedia(\Spatie\MediaLibrary\Models\Media $media): array
return $media->toArray();
}

/**
* @deprecated not needed, field recognizes single/multi file media by itself
*/
public function multiple(): self
{
return $this;
}

/**
* @deprecated
* @see conversionOnIndexView
*/
public function thumbnail(string $conversionOnIndexView): self
{
return $this->withMeta(compact('conversionOnIndexView'));
}

/**
* @deprecated
* @see conversionOnPreview
*/
public function conversion(string $conversionOnPreview): self
{
return $this->withMeta(compact('conversionOnPreview'));
}

/**
* @deprecated
* @see conversionOnDetailView
*/
public function conversionOnView(string $conversionOnDetailView): self
{
return $this->withMeta(compact('conversionOnDetailView'));
}
/**
* @deprecated not needed, field recognizes single/multi file media by itself
*/
public function multiple(): self
{
return $this;
}

/**
* @deprecated
* @see conversionOnIndexView
*/
public function thumbnail(string $conversionOnIndexView): self
{
return $this->withMeta(compact('conversionOnIndexView'));
}

/**
* @deprecated
* @see conversionOnPreview
*/
public function conversion(string $conversionOnPreview): self
{
return $this->withMeta(compact('conversionOnPreview'));
}

/**
* @deprecated
* @see conversionOnDetailView
*/
public function conversionOnView(string $conversionOnDetailView): self
{
return $this->withMeta(compact('conversionOnDetailView'));
}
}

0 comments on commit a792a30

Please sign in to comment.