From: Vincent Vanwaelscappel Date: Wed, 19 Jan 2022 12:09:07 +0000 (+0100) Subject: wip #4804 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=fde4248a28ba2f1aba760fcdffb7483ce2481c46;p=cubist_cms-back.git wip #4804 @1 --- diff --git a/src/app/Magic/EntityData.php b/src/app/Magic/EntityData.php index 1a93273..511921f 100644 --- a/src/app/Magic/EntityData.php +++ b/src/app/Magic/EntityData.php @@ -294,23 +294,7 @@ class EntityData implements \ArrayAccess */ public function getImageURLbyCollection($collectionID, $conversionName = '', $default = null) { - $collectionID = $collectionID ?? ''; - - $media = $this->getMediaByCollection($collectionID); - if (!$media) { - return $default; - } - - foreach ($media as $m) { - /** @var Media $m */ - $res = $m->getUrl($conversionName); - if (!$res) { - continue; - } - return $res; - } - - return $default; + return $this->getEntity()->getImageURLbyCollection($collectionID, $conversionName, $default); } /** @@ -329,17 +313,7 @@ class EntityData implements \ArrayAccess */ 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; + return $this->getEntity()->getMediaPathsByCollection($collectionID, $conversionName); } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index f4e8b76..3811b62 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -33,6 +33,7 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; +use Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Venturecraft\Revisionable\RevisionableTrait; @@ -991,4 +992,73 @@ class CubistMagicAbstractModel extends Model implements HasMedia $field->getFilterOptions(); } } + + /** + * @param string $collectionID Name of the collection + * @param string $conversionName + * @param mixed $default + * @return string|null + */ + public function getImageURLbyCollection($collectionID, $conversionName = '', $default = null) + { + $collectionID = $collectionID ?? ''; + + $media = $this->getMedia($collectionID); + if (!$media) { + return $default; + } + + foreach ($media as $m) { + /** @var Media $m */ + $res = $m->getUrl($conversionName); + if (!$res) { + continue; + } + return $res; + } + + return $default; + } + + /** + * @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; + } + + /** + * @param $collectionID + * @return MediaCollection + */ + public function getMediaByCollection($collectionID) + { + return $this->getMedia($collectionID); + } + + /** + * @param string $fieldName + * @return Media|null + */ + public function getFirstMediaInField(string $fieldName): ?Media + { + $collection = $this->getMediaInField($fieldName); + if ($collection->count() > 0) { + return $collection->first(); + } + return null; + } }