Add an Event bundle class

This commit is contained in:
Oliver Davies 2025-06-12 02:10:08 +01:00
parent f56eb931c2
commit 14cd79a960
6 changed files with 49 additions and 14 deletions

View file

@ -4,20 +4,20 @@ declare(strict_types=1);
namespace Drupal\opd_presentations\Collection;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\opd_presentations\Entity\Event;
/**
* @implements \IteratorAggregate<ParagraphInterface>
* @implements \IteratorAggregate<Event>
*/
readonly final class EventCollection implements \IteratorAggregate {
/**
* @param ParagraphInterface[] $events
* @param Event[] $events
*/
public function __construct(private array $events) {
}
public function first(): ParagraphInterface {
public function first(): Event {
return array_values($this->events)[0];
}

View file

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations\Entity;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
final class Event extends Paragraph implements ParagraphInterface {
public const PARAGRAPH_TYPE = 'event';
public function getEventDate(): string {
/** @var non-empty-string */
return $this->get('field_date')->value;
}
public function getEventName(): string {
/** @var non-empty-string */
return $this->get('field_event_name')->value;
}
}

View file

@ -8,7 +8,7 @@ use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\opd_presentations\Collection\EventCollection;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\opd_presentations\Entity\Event;
final class Presentation extends Node implements NodeInterface {
@ -19,7 +19,7 @@ final class Presentation extends Node implements NodeInterface {
return new EventCollection(array_filter(
array: $events,
callback: fn (Paragraph $event): bool => $event->get('field_date')->value < $today,
callback: fn (Event $event): bool => $event->getEventDate() < $today,
));
}