Is there a way to make ‘current event’ listings move to a ‘past events’ listing page once the date has passed?
Easiest way instead of actually physically moving the pages would be to create a past events page with a model that has the past events as virtual children
But yes, you could also run a cron job (once a day or whatever seems fit) that moves past events to the past-events folder.
How do I setup a page with virtual children?
My current ‘Upcoming Events’ page pulls in the listings using the below code. This shows ALL events past, present and future dates. How do I change this to just show events which are from a ** present and future date**.
<?php foreach ($page->children()->listed() as $event): ?>
And then on the Past Events page, I want to show the same but with just the events which are from a past date.
You don’t even need the virtual children, you can simply filter by date, see our filtering compendium:
That looks perfect - thanks.
One other issue I have realised is the URL of the Events. If two events with identical title exist, how do I make the URL unique. It would be good to automatically include the current month and year in the URL.
mywebsite.com/events/swimming-gala-26-10-2023
or
mywebsite.com/events/2023/10/swimming-gala
The URL is created based on the page slug, therefore two events with the same URL can never exist, because Kirby prevents that. Unless you create your events directly in the file system?
So is there no way the URL to be changed for events to create a file with Year / Month / Event-Title when creating a new Event?
I would have thought this is standard practice when adding a Blog Post to a website?
This method almost works by creating a event.php model
<?php
use Kirby\Cms\Page;
use Kirby\Toolkit\Str;
class EventPage extends Page
{
public static function create(array $props): Page
{
$page = new Page($props);
if ( kirby()->page( $page->id() ) ) {
$props['slug'] = $props['slug'] . '-' . Str::random(3);
}
return parent::create($props);
}
}
This adds 3 random characters to my folder ie: events/swimming-gala-fgh
Is there a way to add the ‘date’ field in here instead of the ‘random’ ?
I have tried adding [date] in like below the code but I get the error Error: Undefined array key “date” when trying to create a new page.
`
$props[‘slug’] = $props[‘date’][‘slug’] . ‘-’ . Str::random(3);
`
Or another option would be to just add a sequential number. So if the URL already exists add a 1, then a 2, then a 3 and so on
At page creation, you don’t have a date in your content yet, right? you could simply add the current date date('Y-m-d')
That would be an option. How would I update the above to include that?
OR as an alternative - How would I update it so it added a sequential number to the end instead of a random number?
$props['slug'] = $props['slug'] . '-' . date('Y-m-d'));
Sequential number would be more complex, as you’d need to go through all pages with the same slug minus the number, then get the numbers from those slugs, then the highest and add 1.