Hi there!
My colleague and I have implemented a multi-page setup consisting of three pages. Each page has its own event pages. Our current goal is as follows:
We want each page to export events under a specific route (‘export/events’) in JSON format. This is already working seamlessly. We’ve written a small plugin that returns the events of the current page as JSON under the specified route, based on a toggle named ‘export’:
App::plugin('abc/events', [
'routes' => [
[
'pattern' => '/export/events',
'method' => 'GET',
'action' => function(){
$local_events = kirby()->collection('events-local')->filterBy('export', 'true');
$data = [];
foreach($local_events as $event) {
$data[] = [
'title' => (string)$event->title(),
'website' => (string)site()->title(),
'uuid' => (string)$event->uuid(),
'categories' => $event->categories()->value(),
'eventDate' => $event->eventDate()->value(),
'eventDateEnd' => $event->eventDateEnd()->value(),
'state' => $event->state()->value(),
'vacations' => $event->vacations()->value(),
'destination' => $event->destination()->value(),
'priceInfo' => $event->priceInfo()->value(),
];
}
return Kirby\Http\Response::json($data, 200, true);
}
],
We then thought it would be useful to have an “import” page on each of the three sites. We plan to extend it in a page model and overwrite the children()
function, so that we can consolidate the imported events in one location.
Here’s the problem: The documentation states that when creating virtual pages from an API—which is essentially what we’re doing with our JSON endpoint—we must set the UUID to prevent Kirby from generating the page in the file system. However, when our ImportPage
model runs, the pages are still being created in the file system, which doesn’t seem correct to us.
Here’s our ImportPage
model:
<?php
use Kirby\Cms\Pages;
use Kirby\Cms\Page;
use Kirby\Http\Remote;
use Kirby\Toolkit\Str;
use Kirby\Uuid\Uuid;
class ImportPage extends Page {
public function children() {
if ($this->children instanceof Pages) {
return $this->children;
}
$pages = [];
if (!kirby()->option('feeds')) { // Check if the feeds option is available
throw new Exception('No feeds to fetch from defined in the config.');
}
foreach (kirby()->option('feeds') as $feedUrl) {
// Fetch events
try {
$request = Remote::get($feedUrl . '/export/events', ['ca' => false]);
if ($request->code() !== 200) {
throw new Exception("Failed to fetch events, status code: " . $request->code());
}
$eventsFeed = $request->json(false);
foreach ($eventsFeed as $key => $event) {
$pages[] = [
'slug' => Str::slug($event->title),
'num' => $key + 1,
'template' => 'event-imported',
'content' => [
'uuid' => $event->uuid,
'website' => $event->website,
'title' => $event->title,
'categories' => $event->categories,
]
];
}
} catch (Exception $e) {
error_log($e->getMessage());
}
}
return $this->children = Pages::factory($pages, $this);
}
}
Thank you for advice in advance!