]> _ Git - cubist_cms-back.git/commitdiff
wip #4804 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 19 Jan 2022 12:09:07 +0000 (13:09 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 19 Jan 2022 12:09:07 +0000 (13:09 +0100)
src/app/Magic/EntityData.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index 1a93273a27926718ada20e51e1d0277ceb68595c..511921f043f203f411ab44e34ed7f695d4c40ec8 100644 (file)
@@ -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);
     }
 
 
index f4e8b7607c1dcf9069b7333b6f072c3e50137417..3811b62ccaa5d31ca75a28fde3362cea66dcedbd 100644 (file)
@@ -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;
+    }
 }