From 47746d3313b3198c6217dde3bf9e8f61c37e35d1 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 25 Jan 2018 16:37:21 -0700 Subject: [PATCH] Added new Collection::toExtendedArray() --- CHANGELOG.md | 2 ++ system/src/Grav/Common/Page/Collection.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e30c87972..73ecc00fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.4.0-rc.2 ## mm/dd/2018 +1. [](#new) + * Added new `Collection:toExtendedArray()` method that's particularly useful for Json output of data 1. [](#improved) * Better `Page.collection()` filtering support including ability to have non-published pages in collections diff --git a/system/src/Grav/Common/Page/Collection.php b/system/src/Grav/Common/Page/Collection.php index 6f476ffce..5113e811f 100644 --- a/system/src/Grav/Common/Page/Collection.php +++ b/system/src/Grav/Common/Page/Collection.php @@ -610,4 +610,23 @@ public function ofOneOfTheseAccessLevels($accessLevels) return $this; } + + /** + * Get the extended version of this Collection with each page keyed by route + * + * @return array + * @throws \Exception + */ + public function toExtendedArray() + { + $items = []; + foreach ($this->items as $path => $slug) { + $page = $this->pages->get($path); + + if ($page !== null) { + $items[$page->route()] = $page->toArray(); + } + } + return $items; + } }