From 809bc343e25f9a4476f46614e81992f912e707a7 Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Mon, 15 Jul 2019 15:13:12 +0200 Subject: [PATCH] Added getHrefByID function to Menu Items so we can avoid exceptions when menu item isn't found by the ID. Also added getImageURLbyCollection function to PageData so we can fetch image URL by specifying collection name instead of the data offset (sometimes it's not practical to give the offset). WIP #2738 @0.5 --- src/app/Magic/Menu/Item.php | 17 +++++++++++++++++ src/app/Magic/PageData.php | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php index de32d59..1b78c22 100644 --- a/src/app/Magic/Menu/Item.php +++ b/src/app/Magic/Menu/Item.php @@ -264,6 +264,23 @@ class Item } + /** + * @param $id + * @param null $default + * @return string|null + */ + public function getHrefByID($id, $default = null) + { + $item = $this->findOneById($id); + + if ($item) { + return $item->getHref(); + } + + return $default; + } + + /** * @param string $href */ diff --git a/src/app/Magic/PageData.php b/src/app/Magic/PageData.php index 802b8bd..f00d4b6 100644 --- a/src/app/Magic/PageData.php +++ b/src/app/Magic/PageData.php @@ -231,6 +231,31 @@ class PageData 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->getEntity()->getMedia($collectionID); + if (!$media) { + return $default; + } + + foreach ($media as $m) { + /** @var Media $m */ + $res = $m->getUrl($conversionName); + if (!$res) { + continue; + } + return $res; + } + + return $default; + } + public function getImageURLAt($offset, $at = 0, $conversionName = '', $default = null) { $media = $this->getMedia($offset); -- 2.39.5