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:
parent
3025ab0f68
commit
06c3da1880
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Drupal\opdavies_blog\Entity\Post;
|
||||||
use Drupal\opdavies_blog\Repository\PostRepository;
|
use Drupal\opdavies_blog\Repository\PostRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,7 +17,7 @@ function opdavies_blog_update_8001(): void {
|
||||||
$posts = \Drupal::service(PostRepository::class)->getAll();
|
$posts = \Drupal::service(PostRepository::class)->getAll();
|
||||||
|
|
||||||
foreach ($posts as $post) {
|
foreach ($posts as $post) {
|
||||||
$post->set('field_sent_to_social_media', TRUE);
|
$post->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE);
|
||||||
$post->save();
|
$post->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,14 @@ use Illuminate\Support\Collection;
|
||||||
*/
|
*/
|
||||||
class Post extends Node implements ContentEntityBundleInterface {
|
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 {
|
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()
|
? $link->getValue()
|
||||||
: NULL;
|
: NULL;
|
||||||
}
|
}
|
||||||
|
@ -30,15 +36,15 @@ class Post extends Node implements ContentEntityBundleInterface {
|
||||||
* @return Collection|Term[]
|
* @return Collection|Term[]
|
||||||
*/
|
*/
|
||||||
public function getTags(): Collection {
|
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 {
|
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 {
|
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 {
|
public function isExternalPost(): bool {
|
||||||
|
@ -46,11 +52,11 @@ class Post extends Node implements ContentEntityBundleInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTags(array $tags): void {
|
public function setTags(array $tags): void {
|
||||||
$this->set('field_tags', $tags);
|
$this->set(self::FIELD_TAGS, $tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shouldSendToSocialMedia(): bool {
|
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 {
|
public function toTweet(): string {
|
||||||
|
|
|
@ -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();
|
$entity->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,9 +27,9 @@ final class PostFactory {
|
||||||
});
|
});
|
||||||
|
|
||||||
$values = [
|
$values = [
|
||||||
'field_tags' => $this->tags->toArray(),
|
|
||||||
'title' => $this->title,
|
'title' => $this->title,
|
||||||
'type' => 'post',
|
'type' => 'post',
|
||||||
|
Post::FIELD_TAGS => $this->tags->toArray(),
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var Post $post */
|
/** @var Post $post */
|
||||||
|
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Drupal\Tests\custom\Kernel\Entity\Node;
|
namespace Drupal\Tests\custom\Kernel\Entity\Node;
|
||||||
|
|
||||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||||
|
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||||
use Drupal\opdavies_blog_test\Factory\PostFactory;
|
use Drupal\opdavies_blog_test\Factory\PostFactory;
|
||||||
|
|
||||||
final class PostTest extends EntityKernelTestBase {
|
final class PostTest extends EntityKernelTestBase {
|
||||||
|
@ -30,7 +31,7 @@ final class PostTest extends EntityKernelTestBase {
|
||||||
|
|
||||||
$this->assertFalse($post->hasTweet());
|
$this->assertFalse($post->hasTweet());
|
||||||
|
|
||||||
$post = (new PostFactory())->create(['field_has_tweet' => TRUE]);
|
$post = (new PostFactory())->create([Post::FIELD_HAS_TWEET => TRUE]);
|
||||||
$post->save();
|
$post->save();
|
||||||
|
|
||||||
$this->assertTrue($post->hasTweet());
|
$this->assertTrue($post->hasTweet());
|
||||||
|
|
|
@ -42,8 +42,8 @@ final class ReorderBlogTagsTest extends EntityKernelTestBase {
|
||||||
$this->createTerm($vocabulary, ['name' => 'Symfony']);
|
$this->createTerm($vocabulary, ['name' => 'Symfony']);
|
||||||
|
|
||||||
$post = $this->createNode([
|
$post = $this->createNode([
|
||||||
'field_tags' => [3, 1, 2],
|
|
||||||
'type' => 'post',
|
'type' => 'post',
|
||||||
|
Post::FIELD_TAGS => [3, 1, 2],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/** @var Post $post */
|
/** @var Post $post */
|
||||||
|
|
|
@ -18,24 +18,27 @@ use Illuminate\Support\Collection;
|
||||||
*/
|
*/
|
||||||
class Talk extends Node implements ContentEntityBundleInterface {
|
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 {
|
public function addEvent(ParagraphInterface $event): void {
|
||||||
$this->set(
|
$this->set(
|
||||||
'field_events',
|
self::FIELD_EVENTS,
|
||||||
$this->getEvents()->push($event)->toArray()
|
$this->getEvents()->push($event)->toArray()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getEvents(): Collection {
|
public function getEvents(): Collection {
|
||||||
return Collection::make($this->get('field_events')
|
return Collection::make($this->get(self::FIELD_EVENTS)
|
||||||
->referencedEntities());
|
->referencedEntities());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getNextDate(): ?int {
|
public function getNextDate(): ?int {
|
||||||
if ($this->get('field_event_date')->isEmpty()) {
|
if ($this->get(self::FIELD_EVENT_DATE)->isEmpty()) {
|
||||||
return NULL;
|
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 {
|
public function setNextDate(int $date): void {
|
||||||
$this->set('field_event_date', $date);
|
$this->set(self::FIELD_EVENT_DATE, $date);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
|
||||||
// If the original event IDs don't match the sorted event IDs, update the
|
// If the original event IDs don't match the sorted event IDs, update the
|
||||||
// event field to use the sorted ones.
|
// event field to use the sorted ones.
|
||||||
if ($events->map->id() != $eventsByDate->map->id()) {
|
if ($events->map->id() != $eventsByDate->map->id()) {
|
||||||
$talk->set('field_events', $eventsByDate->toArray());
|
$talk->set(Talk::FIELD_EVENTS, $eventsByDate->toArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue