Hi there,
I’m building a site that has a list of events. I’d like to put these in date order (‘when’ field), but there is the possibility that an event doesn’t have a date yet (and so outputs “Date TBC”) - I’d like the ones without a date to be at the end of the list, rather than the beginning as they are now. Any help greatly appreciated!
<?php foreach($performances->sortBy('when', 'asc') as $performance): ?>
<li><?php snippet('performancetitle', array('performance' => $performance)) ?></li>
<?php endforeach; ?>
Thanks very much,
Rach
How are you checking the date value exists? You could probably filter on the empty field or use isnotempty()
You can use the map()
method:
<?php
$performances = $performances->map(function($child) {
if($child->when()->isNotEmpty()) {
$child->order = 1;
} else {
$child->order = 2;
}
return $child;
})->sortBy('order', 'asc')->sortBy('when', 'asc');
foreach($performances as $performance): ?>
<li><?php snippet('performancetitle', array('performance' => $performance)) ?></li>
<?php endforeach; ?>
Brilliant thanks for the quick replies @texnixe and @jimbobrjames !
@texnixe I am trying your solution but I have got an error - “Call to a member function order() on null”
This is what I’ve tried:
<?php $performances = page('performances')->children()->visible()->filterBy('year', '*=', $year->title());
$performances = $performances->map(function($child) {
if($child->when()->isNotEmpty()) {
$child->order = 1;
} else {
$child->order = 2;
}
})->sortBy('order', 'asc')->sortBy('when', 'asc');
$count = $performances->count(); ?>
<ul class="ribbon-list leftwards">
<?php foreach($performances as $performance): ?>
<li><?php snippet('performancetitle', array('performance' => $performance)) ?></li>
<?php endforeach; ?>
</ul>
Thank you again for your help!
Rach
Sorry, I forgot to return the child:
$performances = $performances->map(function($child) {
if($child->when()->isNotEmpty()) {
$child->order = 1;
} else {
$child->order = 2;
}
return $child;
})->sortBy('date', 'asc')->sortBy('order');
@texnixe fantastic thank you!
That works perfectly now, I just had to also combine the ‘sortBy’ into one like this:
->sortBy('order', 'asc', 'when', 'asc')
Thank you again for your help! I would have never have got there otherwise 