From cd98df554c5310ec102fcbe9bf27d960049ebe7c Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 19 Mar 2020 19:56:01 +0100 Subject: [PATCH] wip #3511 --- src/app/Magic/EntityData.php | 50 +++++++++++++++---- .../Magic/Models/CubistMagicAbstractModel.php | 5 ++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/app/Magic/EntityData.php b/src/app/Magic/EntityData.php index 0956511..0ab065e 100644 --- a/src/app/Magic/EntityData.php +++ b/src/app/Magic/EntityData.php @@ -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); diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index c671c9b..38e822e 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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); -- 2.39.5