]> _ Git - cubist_cms-back.git/commitdiff
wip #3511
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 19 Mar 2020 18:56:01 +0000 (19:56 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 19 Mar 2020 18:56:01 +0000 (19:56 +0100)
src/app/Magic/EntityData.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index 09565111f27721acf7330e6eb05574309602770b..0ab065e46433a4bae48878f916b5d2a986cb0756 100644 (file)
@@ -271,13 +271,24 @@ class EntityData implements \ArrayAccess
     public function getImageURL($offset, $conversionName = '', $default = null)
     {
         $media = $this->getMedia($offset);
+        return $this->_getURLOfMedia($media, $conversionName, $default);
+    }
+
+    public function getImageAlt($offset, $default = '')
+    {
+        $media = $this->getMedia($offset);
+        return $this->_getAltOfMedia($media, $default);
+    }
+
+    protected function _getAltOfMedia($media, $default = '')
+    {
         if (!$media) {
             return $default;
         }
 
         foreach ($media as $m) {
             /** @var Media $m */
-            $res = $m->getUrl($conversionName);
+            $res = $m->getCustomProperty('alt');
             if (!$res) {
                 continue;
             }
@@ -287,17 +298,8 @@ class EntityData implements \ArrayAccess
         return $default;
     }
 
-    /**
-     * @param string $collectionID Name of the collection
-     * @param string $conversionName
-     * @param mixed $default
-     * @return string|null
-     */
-    public function getImageURLbyCollection($collectionID, $conversionName = '', $default = null)
+    protected function _getURLOfMedia($media, $conversionName, $default = null)
     {
-        $collectionID = $collectionID ?? '';
-
-        $media = $this->getEntity()->getMedia($collectionID);
         if (!$media) {
             return $default;
         }
@@ -314,6 +316,32 @@ class EntityData implements \ArrayAccess
         return $default;
     }
 
+    /**
+     * @param string $collectionID Name of the collection
+     * @param string $conversionName
+     * @param mixed $default
+     * @return string|null
+     */
+    public function getImageURLbyCollection($collectionID, $conversionName = '', $default = null)
+    {
+        $media = $this->_getMediaOfCollection($collectionID);
+        return $this->_getURLOfMedia($media, $conversionName, $default);
+    }
+
+    protected function _getMediaOfCollection($collectionID)
+    {
+        $collectionID = $collectionID ?? '';
+        return $this->getEntity()->getMedia($collectionID);
+    }
+
+
+    public function getImageAltByCollection($collectionID, $default = '')
+    {
+        $media = $this->_getMediaOfCollection($collectionID);
+        return $this->_getAltOfMedia($media, $default);
+    }
+
+
     public function getImageURLAt($offset, $at = 0, $conversionName = '', $default = null)
     {
         $media = $this->getMedia($offset);
index c671c9bc62a01dce6046135071ebe2b07afc984c..38e822e3109c85a56824e811974c9140bbab4b28 100644 (file)
@@ -570,6 +570,11 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         return $this->getMedia($c);
     }
 
+    public function getFirstMediaAlt($collection, $default = '')
+    {
+        return $this->getFirstMedia($collection)->getCustomProperty('alt') ?? $default;
+    }
+
     public function getAttribute($key)
     {
         $value = parent::getAttribute($key);