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.
This commit is contained in:
Oliver Davies 2020-11-10 19:34:15 +00:00
parent 3025ab0f68
commit 06c3da1880
8 changed files with 28 additions and 17 deletions

View file

@ -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();
}
}

View file

@ -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 {

View file

@ -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();
}

View file

@ -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 */

View file

@ -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());

View file

@ -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 */

View file

@ -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);
}
}

View file

@ -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());
}
}