Skip to content

Commit

Permalink
Option to configure filenames ebess#13
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardh authored and ebess committed Oct 25, 2018
1 parent 3cb6182 commit 1e70a89
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/Fields/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
class Images extends Field
{
public $component = 'advanced-media-library-field';

protected $setFileNameCallback;

private $singleImageRules = [];

Expand Down Expand Up @@ -81,9 +83,17 @@ private function addNewImages($data, HasMedia $model, string $collection): Colle
->filter(function ($value) {
return $value instanceof UploadedFile;
})->map(function (UploadedFile $file) use ($model, $collection) {
return $model->addMedia($file)
->toMediaCollection($collection)
->getKey();
$media = $model->addMedia($file);

if(is_callable($this->setFileNameCallback)) {
$media->setFileName(
call_user_func($this->setFileNameCallback, $file->getClientOriginalName(), $file->getClientOriginalExtension(), $model)
);
}

return $media
->toMediaCollection($collection)
->getKey();
});
}

Expand Down Expand Up @@ -130,4 +140,17 @@ public function resolve($resource, $attribute = null)
$this->withMeta(compact('thumbnailUrl'));
}
}

/**
* Set a filename callable callback
*
* @param callable $callback
*
* @return $this
*/
public function setFileName($callback) {
$this->setFileNameCallback = $callback;

return $this;
}
}

0 comments on commit 1e70a89

Please sign in to comment.