I am currently outputting a collection as JSON for consumption for a JavaScript app. It's all working as expected, but now I've hit a wall with related collection content.
I have successfully enabled selection of one of the fields via a drop-down in my primary collection. But when I try to access that on one of my pages, I don't seem to be able to. How can I access this content from the related content for use in my JSON output?
My JSON feed
PHP
- <?php
- // define my feed
- $feed = [
- "mapwidth" => "800",
- "mapheight" => "600",
- "categories" => [],
- "levels" => [[
- "id" => "canada",
- "title" => "Canada",
- "map" => "/src/images/shops-map.svg",
- "locations" => []
- ]]
- ];
- // grab our items from the database
- $locations = perch_collection('Shops', [
- 'sort-order' => 'DESC',
- 'skip-template' => true,
- 'count' => 10,
- ]);
- //loop through the items
- if (count($locations)) {
- foreach($locations as $location) {
- $feed['levels'][0]['locations'][] = (object)[
- 'id' => $location['location'],
- 'title' => $location['name'],
- 'about' => $location['info'],
- "description" => $location['info'],
- "thumbnail" => $location['logo'],
- 'link' => '/shops/'.$location['slug'],
- "x" => "0.2773",
- "y" => "0.2788",
- "zoom" => "1.05"
- ];
- }
- }
- //ready to generate
- header('Content-Type: application/json');
- echo json_encode($feed, JSON_PRETTY_PRINT);
My line 'id' => $location['location'], is not currently able to access this data. If I print the contents of $locations on another page, the content key looks like:
My shop/item.html
Code
- <perch:content id="name" type="text" label="Shop Name" required>
- <perch:content id="slug" type="slug" for="name">
- <perch:content id="logo" type="image" label="Shop Logo" divider-after="Opening Times" required>
- <perch:related id="location" collection="locations" max="1" label="Select location">
- <perch:content id="uniqueid">
- </perch:related>
- <perch:content id="opening_times" type="textarea" label="Shop Opening Times" divider-after="Info" required>
- <perch:content id="info" type="textarea" label="Info" required>