Rename custom module directories
- Rename `opdavies_blog` to `blog`. - Rename `opdavies_blog_test` to `blog_test`. - Rename `opdavies_talks` to `talks`. - Rename `opdavies_talks_test` to `talks_test`. The files within the directories haven't changed, so there is no breaking change caused by renaming the directories. Please enter the commit message for your changes. Lines starting
This commit is contained in:
parent
d7d5a6c8a3
commit
cbe60209e6
59 changed files with 0 additions and 0 deletions
71
web/modules/custom/talks/src/Service/TalkDateUpdater.php
Normal file
71
web/modules/custom/talks/src/Service/TalkDateUpdater.php
Normal file
|
@ -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