From 06c3da18802bb9d8c066aa389737d4afd4f090f1 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 10 Nov 2020 19:34:15 +0000 Subject: [PATCH] Add public constants for field names Make it easier and safer when referencing field names by adding public constants for them to the Talk and Post classes. --- web/modules/custom/blog/opdavies_blog.install | 3 ++- .../custom/blog/src/Entity/Node/Post.php | 18 ++++++++++++------ .../PushBlogPostToSocialMedia.php | 2 +- .../blog_test/src/Factory/PostFactory.php | 2 +- .../tests/src/Kernel/Entity/Node/PostTest.php | 3 ++- .../tests/src/Kernel/ReorderBlogTagsTest.php | 2 +- .../custom/talks/src/Entity/Node/Talk.php | 13 ++++++++----- .../UpdateTalkNodeBeforeSave.php | 2 +- 8 files changed, 28 insertions(+), 17 deletions(-) diff --git a/web/modules/custom/blog/opdavies_blog.install b/web/modules/custom/blog/opdavies_blog.install index b9fc438..5fb9d25 100644 --- a/web/modules/custom/blog/opdavies_blog.install +++ b/web/modules/custom/blog/opdavies_blog.install @@ -7,6 +7,7 @@ declare(strict_types=1); +use Drupal\opdavies_blog\Entity\Post; use Drupal\opdavies_blog\Repository\PostRepository; /** @@ -16,7 +17,7 @@ function opdavies_blog_update_8001(): void { $posts = \Drupal::service(PostRepository::class)->getAll(); foreach ($posts as $post) { - $post->set('field_sent_to_social_media', TRUE); + $post->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE); $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 f6b2eb5..e9ebd26 100644 --- a/web/modules/custom/blog/src/Entity/Node/Post.php +++ b/web/modules/custom/blog/src/Entity/Node/Post.php @@ -20,8 +20,14 @@ use Illuminate\Support\Collection; */ class Post extends Node implements ContentEntityBundleInterface { + public const FIELD_EXTERNAL_LINK = 'field_external_link'; + public const FIELD_HAS_TWEET = 'field_has_tweet'; + public const FIELD_SEND_TO_SOCIAL_MEDIA = 'field_send_to_social_media'; + public const FIELD_SENT_TO_SOCIAL_MEDIA = 'field_sent_to_social_media'; + public const FIELD_TAGS = 'field_tags'; + public function getExternalLink(): ?array { - return ($link = $this->get('field_external_link')->get(0)) + return ($link = $this->get(self::FIELD_EXTERNAL_LINK)->get(0)) ? $link->getValue() : NULL; } @@ -30,15 +36,15 @@ class Post extends Node implements ContentEntityBundleInterface { * @return Collection|Term[] */ public function getTags(): Collection { - return new Collection($this->get('field_tags')->referencedEntities()); + return new Collection($this->get(self::FIELD_TAGS)->referencedEntities()); } public function hasBeenSentToSocialMedia(): bool { - return (bool) $this->get('field_sent_to_social_media')->getString(); + return (bool) $this->get(self::FIELD_SENT_TO_SOCIAL_MEDIA)->getString(); } public function hasTweet(): bool { - return (bool) $this->get('field_has_tweet')->getString(); + return (bool) $this->get(self::FIELD_HAS_TWEET)->getString(); } public function isExternalPost(): bool { @@ -46,11 +52,11 @@ class Post extends Node implements ContentEntityBundleInterface { } public function setTags(array $tags): void { - $this->set('field_tags', $tags); + $this->set(self::FIELD_TAGS, $tags); } public function shouldSendToSocialMedia(): bool { - return (bool) $this->get('field_send_to_social_media')->getString(); + return (bool) $this->get(self::FIELD_SEND_TO_SOCIAL_MEDIA)->getString(); } public function toTweet(): string { diff --git a/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php b/web/modules/custom/blog/src/EventSubscriber/PushBlogPostToSocialMedia.php index 5c3a539..5a33212 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('field_sent_to_social_media', TRUE); + $entity->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE); $entity->save(); } diff --git a/web/modules/custom/blog/tests/modules/blog_test/src/Factory/PostFactory.php b/web/modules/custom/blog/tests/modules/blog_test/src/Factory/PostFactory.php index 5ef0417..ce0df06 100644 --- a/web/modules/custom/blog/tests/modules/blog_test/src/Factory/PostFactory.php +++ b/web/modules/custom/blog/tests/modules/blog_test/src/Factory/PostFactory.php @@ -27,9 +27,9 @@ final class PostFactory { }); $values = [ - 'field_tags' => $this->tags->toArray(), 'title' => $this->title, 'type' => 'post', + Post::FIELD_TAGS => $this->tags->toArray(), ]; /** @var Post $post */ diff --git a/web/modules/custom/blog/tests/src/Kernel/Entity/Node/PostTest.php b/web/modules/custom/blog/tests/src/Kernel/Entity/Node/PostTest.php index ad00379..2e10282 100644 --- a/web/modules/custom/blog/tests/src/Kernel/Entity/Node/PostTest.php +++ b/web/modules/custom/blog/tests/src/Kernel/Entity/Node/PostTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Drupal\Tests\custom\Kernel\Entity\Node; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; +use Drupal\opdavies_blog\Entity\Node\Post; use Drupal\opdavies_blog_test\Factory\PostFactory; final class PostTest extends EntityKernelTestBase { @@ -30,7 +31,7 @@ final class PostTest extends EntityKernelTestBase { $this->assertFalse($post->hasTweet()); - $post = (new PostFactory())->create(['field_has_tweet' => TRUE]); + $post = (new PostFactory())->create([Post::FIELD_HAS_TWEET => TRUE]); $post->save(); $this->assertTrue($post->hasTweet()); diff --git a/web/modules/custom/blog/tests/src/Kernel/ReorderBlogTagsTest.php b/web/modules/custom/blog/tests/src/Kernel/ReorderBlogTagsTest.php index f922dce..77106bf 100644 --- a/web/modules/custom/blog/tests/src/Kernel/ReorderBlogTagsTest.php +++ b/web/modules/custom/blog/tests/src/Kernel/ReorderBlogTagsTest.php @@ -42,8 +42,8 @@ final class ReorderBlogTagsTest extends EntityKernelTestBase { $this->createTerm($vocabulary, ['name' => 'Symfony']); $post = $this->createNode([ - 'field_tags' => [3, 1, 2], 'type' => 'post', + Post::FIELD_TAGS => [3, 1, 2], ]); /** @var Post $post */ diff --git a/web/modules/custom/talks/src/Entity/Node/Talk.php b/web/modules/custom/talks/src/Entity/Node/Talk.php index 7d3ec7a..2697e72 100644 --- a/web/modules/custom/talks/src/Entity/Node/Talk.php +++ b/web/modules/custom/talks/src/Entity/Node/Talk.php @@ -18,24 +18,27 @@ use Illuminate\Support\Collection; */ class Talk extends Node implements ContentEntityBundleInterface { + public const FIELD_EVENTS = 'field_events'; + public const FIELD_EVENT_DATE = 'field_event_date'; + public function addEvent(ParagraphInterface $event): void { $this->set( - 'field_events', + self::FIELD_EVENTS, $this->getEvents()->push($event)->toArray() ); } public function getEvents(): Collection { - return Collection::make($this->get('field_events') + return Collection::make($this->get(self::FIELD_EVENTS) ->referencedEntities()); } public function getNextDate(): ?int { - if ($this->get('field_event_date')->isEmpty()) { + if ($this->get(self::FIELD_EVENT_DATE)->isEmpty()) { return NULL; } - return (int) $this->get('field_event_date')->getString(); + return (int) $this->get(self::FIELD_EVENT_DATE)->getString(); } /** @@ -51,7 +54,7 @@ class Talk extends Node implements ContentEntityBundleInterface { } public function setNextDate(int $date): void { - $this->set('field_event_date', $date); + $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 5319359..3090741 100644 --- a/web/modules/custom/talks/src/EventSubscriber/UpdateTalkNodeBeforeSave.php +++ b/web/modules/custom/talks/src/EventSubscriber/UpdateTalkNodeBeforeSave.php @@ -48,7 +48,7 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface { // 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('field_events', $eventsByDate->toArray()); + $talk->set(Talk::FIELD_EVENTS, $eventsByDate->toArray()); } }