Latest 3 calendar items on homepage

Hello,

I have a page “Calendar” with al my calendar items. I didn’t find the solution to have the latest 3 items on my homepage.
Below you have my code on calendar page.

Can someone help me to solve this?

<?php $calendar = calendar($page->calendar()->yaml()); ?>
<?php date_default_timezone_set('Europe/Brussels'); ?>
<?php
snippet('calendar', array(
	'calendar'	=> $calendar,
	'fields'		=> array(
		'summary'		=> l::get('title'),
		'description'	=> l::get('description'),
		'link'			=> l::get('link')
	)
));
?>

I would be easier if you use the toStructure() method instead of yaml(). Then you could use collection methods like flip(), sort(), limit() etc.

Thx for the reply.
Can you help me with this. It’s the first time I need do that.

Oh, I just realized you are using a plugin. So you can’t do that. Maybe the plugin provides methods for sorting or limiting. If not, you would have to use php array methods.

Which plugin are you using that provides the calendar() function? Could you please post a link to it so that we can take a look at it?

I’m using this one: https://github.com/mzur/kirby-calendar-plugin

You could try the following:

<?php
$calendar = $page->calendar()->toStructure()
                             ->sortBy('_begin_date', 'desc')
                             ->limit(3)
                             ->toArray();
$calendar = calendar($calendar); ?>

If that doesn’t work, please contact the plugin author with a feature request to support Kirby Structure objects in addition to arrays.

Wow, I haven’t touched that plugin in a long time and it’s still in use. In addition to @lukasbestle’s answer you can do something like this in the calendar snippet:

foreach (array_slice($calendar->getEvents(), 0, $count) as $event):

This shows the next $count upcoming events.