Fix the ordering for future talks
Rather than the custom event sorting plugin being based on the `created` value, this change adds a new `field_event_date` field to the talk node type and uses this for the sorting instead. This commit also adds a new `TalkDateUpdater` service that extracts either the next event date if there is a future date, or the last past event date if there is no future date, from `field_events` for each talk and saves it into the event date field. For consistency, and to ensure that the results are ordered correctly, the talk date updater converts the date from a date string (e.g. `2020-08-24`) into a UNIX timestamp, and the timestamp is saved in the event date field. This can be changed at a later date if needed. The talks view has been updated to use the updated sort plugin, and the existing tests have been updated to use the new field. References #204
This commit is contained in:
parent
bdf225b05d
commit
6d9ecd8df0
17 changed files with 340 additions and 21 deletions
71
web/modules/custom/opd_talks/src/Service/TalkDateUpdater.php
Normal file
71
web/modules/custom/opd_talks/src/Service/TalkDateUpdater.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_talks\Service;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\Component\Datetime\TimeInterface;
|
||||
use Drupal\custom\Entity\Node\Talk;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\opd_talks\Repository\TalkRepository;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
|
||||
final class TalkDateUpdater {
|
||||
|
||||
private TalkRepository $talkRepository;
|
||||
private TimeInterface $time;
|
||||
|
||||
public function __construct(
|
||||
TalkRepository $talkRepository,
|
||||
TimeInterface $time
|
||||
) {
|
||||
$this->talkRepository = $talkRepository;
|
||||
$this->time = $time;
|
||||
}
|
||||
|
||||
public function __invoke(): void {
|
||||
foreach ($this->talkRepository->getAll() as $talk) {
|
||||
$this->updateNextEventDate($talk);
|
||||
}
|
||||
}
|
||||
|
||||
private function updateNextEventDate(Talk $talk) {
|
||||
if (!$nextDate = $this->findNextEventDate($talk)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$nextDateTimestamp = Carbon::parse($nextDate)
|
||||
->getTimestamp();
|
||||
|
||||
if ($nextDateTimestamp == $talk->getNextDate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$talk->setNextDate($nextDateTimestamp);
|
||||
$talk->save();
|
||||
}
|
||||
|
||||
private function findNextEventDate(Talk $talk): ?string {
|
||||
$currentTime = Carbon::today()
|
||||
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
|
||||
|
||||
$dates = $talk->getEvents()
|
||||
->map(fn(ParagraphInterface $event) => $event->get('field_date')
|
||||
->getString())
|
||||
->sort();
|
||||
|
||||
if ($dates->isEmpty()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// If a future date is found, return it.
|
||||
if ($futureDate = $dates->first(fn(string $eventDate) => $eventDate > $currentTime)) {
|
||||
return $futureDate;
|
||||
}
|
||||
|
||||
// If no future date is found, return the last past date.
|
||||
return $dates->last();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue