From 8d8bf62f2c3944666a71d78a1deda3dd61118002 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 10 Jun 2025 20:47:23 +0100 Subject: [PATCH] Return an EventCollection --- .../src/Collection/EventCollection.php | 24 +++++++++++++++++++ .../src/Entity/Presentation.php | 7 +++--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 modules/opd_presentations/src/Collection/EventCollection.php diff --git a/modules/opd_presentations/src/Collection/EventCollection.php b/modules/opd_presentations/src/Collection/EventCollection.php new file mode 100644 index 000000000..2a4c62318 --- /dev/null +++ b/modules/opd_presentations/src/Collection/EventCollection.php @@ -0,0 +1,24 @@ + + */ +readonly final class EventCollection implements \IteratorAggregate { + + /** + * @param ParagraphInterface[] $events + */ + public function __construct(private array $events) { + } + + public function getIterator(): \Traversable { + return new \ArrayIterator($this->events); + } + +} diff --git a/modules/opd_presentations/src/Entity/Presentation.php b/modules/opd_presentations/src/Entity/Presentation.php index 4b3e102c4..da7ccde6c 100644 --- a/modules/opd_presentations/src/Entity/Presentation.php +++ b/modules/opd_presentations/src/Entity/Presentation.php @@ -7,19 +7,20 @@ namespace Drupal\opd_presentations\Entity; 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; final class Presentation extends Node implements NodeInterface { - public function getPastEvents(): array { + public function getPastEvents(): EventCollection { $events = $this->get('field_events')->referencedEntities(); $today = (new DrupalDateTime('today'))->format('U'); - return array_filter( + return new EventCollection(array_filter( array: $events, callback: fn (Paragraph $event): bool => $event->get('field_date')->value < $today, - ); + )); } }