From 0f5549ea73757fcc0ad59ce4200d0c9f6198835b Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 19 Oct 2021 20:20:59 +0200 Subject: [PATCH] wip #4804 --- src/app/Magic/EntityData.php | 38 +++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/app/Magic/EntityData.php b/src/app/Magic/EntityData.php index e7f0db9..ae58994 100644 --- a/src/app/Magic/EntityData.php +++ b/src/app/Magic/EntityData.php @@ -5,10 +5,9 @@ namespace Cubist\Backpack\Magic; use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel; -use Cubist\Backpack\Magic\Models\CubistMagicModel; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; -use Spatie\MediaLibrary\Models\Media; +use Spatie\MediaLibrary\MediaCollections\Models\Media; class EntityData implements \ArrayAccess { @@ -22,7 +21,7 @@ class EntityData implements \ArrayAccess protected $_entity; /** - * @return CubistMagicModel + * @return CubistMagicAbstractModel */ public function getEntity(): CubistMagicAbstractModel { @@ -30,7 +29,7 @@ class EntityData implements \ArrayAccess } /** - * @param CubistMagicModel $entity + * @param CubistMagicAbstractModel $entity */ public function setEntity(CubistMagicAbstractModel $entity): void { @@ -297,7 +296,7 @@ class EntityData implements \ArrayAccess { $collectionID = $collectionID ?? ''; - $media = $this->getEntity()->getMedia($collectionID); + $media = $this->getMediaByCollection($collectionID); if (!$media) { return $default; } @@ -314,6 +313,35 @@ class EntityData implements \ArrayAccess return $default; } + /** + * @param $collectionID + * @return \Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection + */ + public function getMediaByCollection($collectionID) + { + return $this->getEntity()->getMedia($collectionID); + } + + /** + * @param $collectionID + * @param string $conversionName + * @return array + */ + public function getMediaPathsByCollection($collectionID, $conversionName = '') + { + if (!$collectionID) { + return []; + } + $media = $this->getMediaByCollection($collectionID); + $res = []; + foreach ($media as $m) { + /** @var Media $m */ + $res[] = $m->getPath($conversionName); + } + return $res; + } + + public function getImageURLAt($offset, $at = 0, $conversionName = '', $default = null) { $media = $this->getMedia($offset); -- 2.39.5