Rename and re-organise custom modules
- Rename `opd_talks` to `opdavies_talks` - Rename `custom` to `opdavies_blog`
This commit is contained in:
parent
e4e898f22c
commit
9b1a8fb3be
53 changed files with 125 additions and 116 deletions
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_talks\Service;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\Component\Datetime\TimeInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
use Drupal\opdavies_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