From a656280e7be484505974fa37efd1854f1a8b59b5 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 10 Nov 2020 19:51:53 +0000 Subject: [PATCH] Extract helper methods --- web/modules/custom/blog/opdavies_blog.install | 3 +-- .../custom/blog/src/Entity/Node/Post.php | 6 ++++++ .../PushBlogPostToSocialMedia.php | 2 +- .../custom/talks/src/Entity/Node/Talk.php | 4 ++++ .../UpdateTalkNodeBeforeSave.php | 20 +++++++++++-------- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/web/modules/custom/blog/opdavies_blog.install b/web/modules/custom/blog/opdavies_blog.install index 5fb9d25..7701b79 100644 --- a/web/modules/custom/blog/opdavies_blog.install +++ b/web/modules/custom/blog/opdavies_blog.install @@ -7,7 +7,6 @@ declare(strict_types=1); -use Drupal\opdavies_blog\Entity\Post; use Drupal\opdavies_blog\Repository\PostRepository; /** @@ -17,7 +16,7 @@ function opdavies_blog_update_8001(): void { $posts = \Drupal::service(PostRepository::class)->getAll(); foreach ($posts as $post) { - $post->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE); + $post->markAsSentToSocialMedia(); $post->save(); } } diff --git a/web/modules/custom/blog/src/Entity/Node/Post.php b/web/modules/custom/blog/src/Entity/Node/Post.php index e9ebd26..3ea094c 100644 --- a/web/modules/custom/blog/src/Entity/Node/Post.php +++ b/web/modules/custom/blog/src/Entity/Node/Post.php @@ -51,6 +51,12 @@ class Post extends Node implements ContentEntityBundleInterface { return (bool) $this->getExternalLink(); } + public function markAsSentToSocialMedia(): self { + $this->set(self::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE); + + return $this; + } + public function setTags(array $tags): void { $this->set(self::FIELD_TAGS, $tags); } diff --git a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php index 5a33212..b95f315 100644 --- a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php +++ b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php @@ -76,7 +76,7 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { ], ]); - $entity->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE); + $entity->markAsSentToSocialMedia(); $entity->save(); } diff --git a/web/modules/custom/talks/src/Entity/Node/Talk.php b/web/modules/custom/talks/src/Entity/Node/Talk.php index 2697e72..195cc4a 100644 --- a/web/modules/custom/talks/src/Entity/Node/Talk.php +++ b/web/modules/custom/talks/src/Entity/Node/Talk.php @@ -53,6 +53,10 @@ class Talk extends Node implements ContentEntityBundleInterface { ->max(); } + public function setEvents(array $events): void { + $this->set(self::FIELD_EVENTS, $events); + } + public function setNextDate(int $date): void { $this->set(self::FIELD_EVENT_DATE, $date); } diff --git a/web/modules/custom/talks/src/EventSubscriber/UpdateTalkNodeBeforeSave.php b/web/modules/custom/talks/src/EventSubscriber/UpdateTalkNodeBeforeSave.php index 3090741..bcab73e 100644 --- a/web/modules/custom/talks/src/EventSubscriber/UpdateTalkNodeBeforeSave.php +++ b/web/modules/custom/talks/src/EventSubscriber/UpdateTalkNodeBeforeSave.php @@ -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); } }