Automatically update the created date for talks

Automatically update the created dates for talk nodes so that they match
the most-future event. This means that the talks are ordered correctly
on the Talks page.
This commit is contained in:
Oliver Davies 2020-05-08 18:46:31 +01:00
parent fe5340d1c7
commit 7a9bf80a89
18 changed files with 412 additions and 6 deletions

View file

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Drupal\custom\EventSubscriber;
use Drupal\Core\Entity\EntityInterface;
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\paragraphs\ParagraphInterface;
use Illuminate\Support\Collection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Set the created date for a talk to be the last date that the talk is given.
*/
final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
HookEventDispatcherInterface::ENTITY_INSERT => 'entityInsertOrUpdate',
HookEventDispatcherInterface::ENTITY_UPDATE => 'entityInsertOrUpdate',
];
}
public function entityInsertOrUpdate(BaseEntityEvent $event): void {
if ($event->getEntity()->getEntityTypeId() != 'node') {
return;
}
if ($event->getEntity()->bundle() != 'talk') {
return;
}
$this->updateCreatedDate($event->getEntity());
}
private function updateCreatedDate(EntityInterface $talk): void {
if (!$eventDate = $this->findLatestEventDate($talk)) {
return;
}
$talkDate = (new \DateTime($eventDate))->getTimestamp();
if ($talkDate == $talk->get('created')->getString()) {
return;
}
$talk->set('created', $talkDate);
}
/**
* Find the date for the latest event.
*
* @return string|null
*/
private function findLatestEventDate(EntityInterface $talk) {
return Collection::make($talk->get('field_events')->referencedEntities())
->map(fn(ParagraphInterface $event) => $event->get('field_date')
->getString())
->max();
}
}