Extract helper methods
This commit is contained in:
parent
06c3da1880
commit
a656280e7b
5 changed files with 24 additions and 11 deletions
|
@ -9,6 +9,7 @@ use Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent;
|
|||
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
|
@ -39,19 +40,22 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
|
|||
|
||||
private function reorderEvents(Talk $talk): void {
|
||||
$events = $talk->getEvents();
|
||||
|
||||
$eventsByDate = $events
|
||||
->sortBy(fn(ParagraphInterface $event) => $event->get('field_date')
|
||||
->getString())
|
||||
->values();
|
||||
$eventsByDate = $this->sortEventsByDate($events);
|
||||
|
||||
// If the original event IDs don't match the sorted event IDs, update the
|
||||
// event field to use the sorted ones.
|
||||
if ($events->map->id() != $eventsByDate->map->id()) {
|
||||
$talk->set(Talk::FIELD_EVENTS, $eventsByDate->toArray());
|
||||
$talk->setEvents($eventsByDate->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
private function sortEventsByDate(Collection $events): Collection {
|
||||
return $events
|
||||
->sortBy(fn(ParagraphInterface $event) => $event->get('field_date')
|
||||
->getString())
|
||||
->values();
|
||||
}
|
||||
|
||||
private function updateCreatedDate(Talk $talk): void {
|
||||
if (!$eventDate = $talk->findLatestEventDate()) {
|
||||
return;
|
||||
|
@ -59,11 +63,11 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
|
|||
|
||||
$talkDate = Carbon::parse($eventDate)->getTimestamp();
|
||||
|
||||
if ($talkDate == $talk->get('created')->getString()) {
|
||||
if ($talkDate == $talk->getCreatedTime()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$talk->set('created', $talkDate);
|
||||
$talk->setCreatedTime($talkDate);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue