Add logging for updating a talk before saving

This commit is contained in:
Oliver Davies 2024-06-10 22:21:33 +01:00
parent 13071b2ae6
commit cd9b8f0585
2 changed files with 14 additions and 8 deletions

View file

@ -2,23 +2,18 @@ services:
Drupal\Core\Cache\CacheBackendInterface:
alias: cache.default
private: true
Drupal\opdavies_talks\Repository\TalkRepository:
autowire: true
Drupal\opdavies_talks\Service\CachedTalkCounter:
autowire: true
Drupal\opdavies_talks\Service\TalkCounter:
autowire: true
Drupal\opdavies_talks\Service\TalkCounterInterface:
autowire: true
class: Drupal\opdavies_talks\Service\CachedTalkCounter
Drupal\opdavies_talks\Service\TalkDateUpdater:
autowire: true
Drupal\opdavies_talks\EventSubscriber\UpdateTalkNodeBeforeSave:
autowire: true
tags:
- { name: event_subscriber }
- {name: event_subscriber}

View file

@ -4,12 +4,13 @@ declare(strict_types=1);
namespace Drupal\opdavies_talks\EventSubscriber;
use Carbon\Carbon;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\core_event_dispatcher\EntityHookEvents;
use Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent;
use Drupal\opdavies_talks\Entity\Node\Talk;
use Drupal\paragraphs\ParagraphInterface;
use Illuminate\Support\Collection;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
@ -17,6 +18,12 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*/
final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
private LoggerInterface $logger;
public function __construct(LoggerChannelFactoryInterface $logger) {
$this->logger = $logger->get('opdavies_talks');
}
public static function getSubscribedEvents() {
return [
EntityHookEvents::ENTITY_PRE_SAVE => 'onEntityPreSave',
@ -45,6 +52,8 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
return;
};
$this->logger->debug(sprintf('Sorting events for talk "%s"', $talk->label()));
$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.
@ -61,6 +70,8 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
}
private function updateMostRecentEventDate(Talk $talk): void {
$this->logger->debug(sprintf('Updating the most recent event date for talk "%s"', $talk->label()));
$mostRecentEventDate = $talk->findLatestEventDate();
$talk->set('field_event_date', $mostRecentEventDate);