-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.2] UploadableImage trait for Spatie MediaLibrary (#32)
* UploadableImage trait for Spatie MediaLibrary * Update README.md for UploadableImage
- Loading branch information
Showing
3 changed files
with
186 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Novius\Backpack\CRUD\ModelTraits\SpatieMediaLibrary; | ||
|
||
use Novius\Backpack\CRUD\ModelTraits\UploadableImage as UploadableImageOriginal; | ||
|
||
/** | ||
* Trait UploadableImage | ||
* | ||
* To make media upload work : | ||
* - implement the trait Spatie\MediaLibrary\HasMedia\HasMediaTrait on your model | ||
* - call $this->setUploadedImage($value) in your model attribute mutator | ||
* | ||
* @package Novius\Backpack\CRUD\ModelTraits\SpatieMediaLibrary | ||
*/ | ||
trait UploadableImage | ||
{ | ||
use UploadableImageOriginal { | ||
imagePathSaved as imagePathSavedOriginal; | ||
imagePathDeleted as imagePathDeletedOriginal; | ||
} | ||
|
||
/** | ||
* Callback triggered after image saved on disk | ||
* | ||
* @param string $imageAttributeName | ||
* @param string|null $imagePath | ||
* @param string|null $diskName | ||
* @return bool | ||
*/ | ||
public function imagePathSaved(string $imagePath, string $imageAttributeName = null, string $diskName = null) : bool | ||
{ | ||
// Adds the image to the medialibrary | ||
$this->addMedia($imagePath) | ||
->preservingOriginal() | ||
->toMediaCollection($this->getImageCollectionName($imageAttributeName)); | ||
|
||
return $this->imagePathSavedOriginal($imagePath, $imageAttributeName, $diskName); | ||
} | ||
|
||
/** | ||
* Callback triggered after image deleted on disk | ||
* | ||
* @param string $imagePath | ||
* @param string|null $imageAttributeName | ||
* @param string|null $diskName | ||
* @return bool | ||
*/ | ||
public function imagePathDeleted(string $imagePath, string $imageAttributeName = null, string $diskName = null) : bool | ||
{ | ||
// Removes the image from the medialibrary | ||
$this->clearMediaCollection($this->getImageCollectionName($imageAttributeName)); | ||
|
||
return $this->imagePathDeletedOriginal($imagePath, $imageAttributeName, $diskName); | ||
} | ||
|
||
/** | ||
* Gets the localized image attribute name | ||
* | ||
* @param string $imageAttributeName | ||
* @param string|null $locale | ||
* @return string | ||
*/ | ||
public function getImageCollectionName(string $imageAttributeName, string $locale = null) | ||
{ | ||
$collectionName = $imageAttributeName; | ||
|
||
// Appends the locale if translatable | ||
if ($this->isTranslatableImageAttribute($imageAttributeName)) { | ||
$collectionName .= '-'.($locale ?? $this->getLocale()); | ||
} | ||
|
||
return $collectionName; | ||
} | ||
} |
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