2020-05-08 18:46:31 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Drupal\custom\EventSubscriber;
|
|
|
|
|
2020-05-09 21:51:04 +01:00
|
|
|
use Carbon\Carbon;
|
2020-05-08 18:46:31 +01:00
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
2020-05-09 22:31:34 +01:00
|
|
|
use Drupal\custom\Entity\Node;
|
2020-05-08 18:46:31 +01:00
|
|
|
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
|
|
|
|
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
|
|
|
|
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 {
|
2020-05-10 13:15:07 +01:00
|
|
|
if ($event->getEntity()->getEntityTypeId() != 'node') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($event->getEntity()->bundle() != 'talk') {
|
2020-05-08 18:46:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->updateCreatedDate($event->getEntity());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function updateCreatedDate(EntityInterface $talk): void {
|
2020-05-09 22:31:34 +01:00
|
|
|
/** @var \Drupal\custom\Entity\Node $talk */
|
|
|
|
if (!$eventDate = $talk->findLatestEventDate()) {
|
2020-05-08 18:46:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:51:04 +01:00
|
|
|
$talkDate = Carbon::parse($eventDate)->getTimestamp();
|
2020-05-08 18:46:31 +01:00
|
|
|
|
|
|
|
if ($talkDate == $talk->get('created')->getString()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$talk->set('created', $talkDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|