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

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