From 1a2372f6110f7c08835af9907455d1d196d562ca Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 9 May 2020 22:31:34 +0100 Subject: [PATCH] Add and use a custom Node class Add and use a custom `Node` class within the custom module, and move the method there for finding the last event date for a talk node. --- phpstan.neon | 2 +- web/modules/custom/custom/custom.module | 19 ++++++++++++ web/modules/custom/custom/src/Entity/Node.php | 29 +++++++++++++++++++ .../UpdateTalkCreatedDateOnSave.php | 24 +++------------ 4 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 web/modules/custom/custom/custom.module create mode 100644 web/modules/custom/custom/src/Entity/Node.php diff --git a/phpstan.neon b/phpstan.neon index 7aa084a..21de235 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,8 +8,8 @@ parameters: - *TestBase.php checkMissingIterableValueType: false ignoreErrors: - - '#Call to an undefined method Drupal\\Core\\Entity\\EntityInterface::get()#' - '#Call to an undefined method Drupal\\Core\\Entity\\EntityInterface::set()#' + - '#Call to an undefined method Drupal\\Core\\Field\\FieldItemListInterface::referencedEntities()#' includes: - vendor/mglaman/phpstan-drupal/extension.neon - vendor/phpstan/phpstan-deprecation-rules/rules.neon diff --git a/web/modules/custom/custom/custom.module b/web/modules/custom/custom/custom.module new file mode 100644 index 0000000..1d94853 --- /dev/null +++ b/web/modules/custom/custom/custom.module @@ -0,0 +1,19 @@ +setClass(Node::class); + } +} diff --git a/web/modules/custom/custom/src/Entity/Node.php b/web/modules/custom/custom/src/Entity/Node.php new file mode 100644 index 0000000..eecbb67 --- /dev/null +++ b/web/modules/custom/custom/src/Entity/Node.php @@ -0,0 +1,29 @@ +hasField('field_events')) { + return NULL; + } + + return Collection::make($this->get('field_events')->referencedEntities()) + ->map(fn(ParagraphInterface $event) => $event->get('field_date') + ->getString()) + ->max(); + } + +} diff --git a/web/modules/custom/custom/src/EventSubscriber/UpdateTalkCreatedDateOnSave.php b/web/modules/custom/custom/src/EventSubscriber/UpdateTalkCreatedDateOnSave.php index fa8e0cf..b258802 100644 --- a/web/modules/custom/custom/src/EventSubscriber/UpdateTalkCreatedDateOnSave.php +++ b/web/modules/custom/custom/src/EventSubscriber/UpdateTalkCreatedDateOnSave.php @@ -6,10 +6,9 @@ namespace Drupal\custom\EventSubscriber; use Carbon\Carbon; use Drupal\Core\Entity\EntityInterface; +use Drupal\custom\Entity\Node; 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; /** @@ -25,11 +24,7 @@ final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface { } public function entityInsertOrUpdate(BaseEntityEvent $event): void { - if ($event->getEntity()->getEntityTypeId() != 'node') { - return; - } - - if ($event->getEntity()->bundle() != 'talk') { + if (!$event->getEntity() instanceof Node) { return; } @@ -37,7 +32,8 @@ final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface { } private function updateCreatedDate(EntityInterface $talk): void { - if (!$eventDate = $this->findLatestEventDate($talk)) { + /** @var \Drupal\custom\Entity\Node $talk */ + if (!$eventDate = $talk->findLatestEventDate()) { return; } @@ -50,16 +46,4 @@ final class UpdateTalkCreatedDateOnSave implements EventSubscriberInterface { $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(); - } - }