Pre-format information from page fields for a clean template

Oh yeah – that’s true! It’s still using the old home.php controller, I had commented out the part that was throwing the error. Sent you the updated file!

Hm, yes, ok, that doesn’t look good. I don’t know what’s happening there.

@bruno It just doesn’t work with groupBy(), no idea why, but does using group():

<?php

date_default_timezone_set('Europe/Vienna');

return function($site, $pages, $page) {

    // $groups = page('exhibitions')->children()->visible()->sortBy('startDate', 'desc')->group(function($exhibition) {
    //   $now = new DateTime('now');
    //   $start = new DateTime($exhibition->startDate());
    //   $end = new DateTime($exhibition->endDate());
    //
    //   if ($start > $now)                  return 'Upcoming';
    //   if ($start < $now && $end > $now)   return 'Current';
    //   return 'Past';
    //
    // });

  $groups = page('exhibitions')
    ->children()
    ->visible()
    ->sortBy('startDate', 'desc')
    ->map(function($exhibition) {

      $now = new DateTime('now');
  $start = new DateTime($exhibition->startDate());
  $end = new DateTime($exhibition->endDate());

  if ($start > $now)    {
    $exhibition->category =  'Upcoming';
    $exhibition->sortOrder = 0;
  }
  if ($start < $now && $end > $now)  {
     $exhibition->category =  'Current';
      $exhibition->sortOrder = 1;
  } else {
    $exhibition->category =  'Past';
     $exhibition->sortOrder = 2;
  }
  return $exhibition;
  })->sortBy('sortOrder', 'asc')->group(function($item) {
    return $item->category();
  });



  return [
    'groups' => $groups,

  ];

};

Note that there’s an undefined variable in your home template ($time)

Also, I removed the additional mapping stuff at the end because I don’t know what it was there for.

Thank you! Hmm, the code is not “broken” anymore but it is putting all exhibitions into the “Past”-category and sorting them alphabetically by title… did you change anything in any other files as well?
($time = $description is defined on line 14 – the mapping stuff was just some commented out trials from before.)

Ah, I missed the logic error when I pasted here:

  if ($start > $now)    {
    echo $exhibition->title();
    $exhibition->category =  'Upcoming';
    $exhibition->sortOrder = 0;
  } elseif ($start < $now && $end > $now)  { # this must be else if
     $exhibition->category =  'Current';
      $exhibition->sortOrder = 1;
  } else {
    $exhibition->category =  'Past';
     $exhibition->sortOrder = 2;
  }
  return $exhibition;
  })->group(function($item) {
    return $item->category();
  })->sortBy('sortOrder', 'asc');

I think you also had some manual sortorder fields in your content files that might mess things up.

Oh of course! The grouping works now – but the exhibitions are still being sorted alphabetically instead of chronologically.
I added the sortorder fields because all content needed to have the field defined in order for the code to work. They all have a value of 1

I made a change above, the sortBy('sortOrder') bit should be at the end, so that it applies to the groups, not the items. The items themselves are already sorted at the top before the mapping stuff.

:+1: Thank you sooo much for helping me with this!