2020-05-09 22:31:34 +01:00
|
|
|
<?php
|
|
|
|
|
2020-08-24 09:26:44 +01:00
|
|
|
namespace Drupal\opdavies_talks\Entity\Node;
|
2020-05-09 22:31:34 +01:00
|
|
|
|
2020-05-29 21:48:45 +01:00
|
|
|
use Drupal\discoverable_entity_bundle_classes\ContentEntityBundleInterface;
|
|
|
|
use Drupal\node\Entity\Node;
|
2020-05-09 22:31:34 +01:00
|
|
|
use Drupal\paragraphs\ParagraphInterface;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
2020-05-29 21:48:45 +01:00
|
|
|
/**
|
|
|
|
* Defines an talk node class.
|
|
|
|
*
|
|
|
|
* @ContentEntityBundleClass(
|
|
|
|
* label = @Translation("Talk"),
|
|
|
|
* entity_type = "node",
|
|
|
|
* bundle = "talk"
|
|
|
|
* );
|
|
|
|
*/
|
|
|
|
class Talk extends Node implements ContentEntityBundleInterface {
|
2020-05-09 22:31:34 +01:00
|
|
|
|
2020-05-29 02:30:29 +01:00
|
|
|
public function addEvent(ParagraphInterface $event): void {
|
|
|
|
$this->set(
|
|
|
|
'field_events',
|
|
|
|
$this->getEvents()->push($event)->toArray()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEvents(): Collection {
|
|
|
|
return Collection::make($this->get('field_events')
|
|
|
|
->referencedEntities());
|
|
|
|
}
|
|
|
|
|
2020-08-24 02:00:22 +01:00
|
|
|
public function getNextDate(): ?int {
|
|
|
|
if ($this->get('field_event_date')->isEmpty()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) $this->get('field_event_date')->getString();
|
|
|
|
}
|
|
|
|
|
2020-05-09 22:31:34 +01:00
|
|
|
/**
|
|
|
|
* Find the date for the latest event.
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function findLatestEventDate(): ?string {
|
2020-05-29 02:30:29 +01:00
|
|
|
return $this->getEvents()
|
2020-05-09 22:31:34 +01:00
|
|
|
->map(fn(ParagraphInterface $event) => $event->get('field_date')
|
|
|
|
->getString())
|
|
|
|
->max();
|
|
|
|
}
|
|
|
|
|
2020-08-24 02:00:22 +01:00
|
|
|
public function setNextDate(int $date): void {
|
|
|
|
$this->set('field_event_date', $date);
|
|
|
|
}
|
|
|
|
|
2020-05-09 22:31:34 +01:00
|
|
|
}
|