Events and transition to the coming year

Hello

I have to build a list of events sorted by date. Inside this list I have to insert a div that warns the transition to the coming year.

  • Event 01.01.2021
  • Event 02.02.2021
  • Event 03.03.2021
  • >> Hey this is 2021
  • Event 01.01.2022
  • Event 02.02.2022
  • Event 03.03.2022
  • >> Hey this is 2022

A the moment, I have simple lines like

<?php foreach($articles->sortBy('datedebut', 'asc') as $article): ?>
...
<?php endforeach ?>

I can’t figure out how I could do, do you have any tips?

I think it would make sense to group your events by year (see groupBy: $pages->groupBy() | Kirby CMS)

Thank you texnixe ! Now I’m grouping my events by year and I sort them. This gives me (as expected) the same result, but I still don’t know how to detect the last entry of each year to announce the next one

 <?php 

    $callback = function($p) {
      return $p->datedebut()->toDate('Y');
    };
    $groupedItems = $articles->group($callback);
    foreach($groupedItems as $year => $itemsPerYear): ?>
   
   <?php foreach($articles->sortBy('datedebut', 'desc') as $article): ?>

      ...

   <?php endforeach ?>
   <?php endforeach ?>

But you don’t need to detect this! After the inner loop, you can announce the next year…

 <?php 

    $callback = function($p) {
      return $p->datedebut()->toDate('Y');
    };
    $groupedItems = $articles->group($callback);
    foreach($groupedItems as $year => $itemsPerYear): ?>
   <!-- Note that for the inner loop you need to loop through $itemsPerYear, not `articles`, or both variables have to be called $articles -->
   <?php foreach($itemsPerYear->sortBy('datedebut', 'desc') as $article): ?>

      <!-- list of events for the given year... -->

   <?php endforeach ?>
   <?php echo 'Here comes the next year'; ?>
   <?php endforeach ?>
1 Like

Of course !!!
Thank you very much Sonja have a nice day !
O.