From 6e19d01eed67ec884908cb4753f318d1a562e3f7 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 10 Jun 2025 20:47:23 +0100 Subject: [PATCH] Get past events for each presentation --- .../src/Entity/Presentation.php | 11 +++++++- .../Entity/PresentationTest.php | 25 ++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/modules/opd_presentations/src/Entity/Presentation.php b/modules/opd_presentations/src/Entity/Presentation.php index 218c15f02..4b3e102c4 100644 --- a/modules/opd_presentations/src/Entity/Presentation.php +++ b/modules/opd_presentations/src/Entity/Presentation.php @@ -4,13 +4,22 @@ declare(strict_types=1); namespace Drupal\opd_presentations\Entity; +use Drupal\Core\Datetime\DrupalDateTime; use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; +use Drupal\paragraphs\Entity\Paragraph; final class Presentation extends Node implements NodeInterface { public function getPastEvents(): array { - return $this->get('field_events')->referencedEntities(); + $events = $this->get('field_events')->referencedEntities(); + + $today = (new DrupalDateTime('today'))->format('U'); + + return array_filter( + array: $events, + callback: fn (Paragraph $event): bool => $event->get('field_date')->value < $today, + ); } } diff --git a/modules/opd_presentations/tests/src/DrupalTestTraits/Entity/PresentationTest.php b/modules/opd_presentations/tests/src/DrupalTestTraits/Entity/PresentationTest.php index 5f755f3fc..d4a563200 100644 --- a/modules/opd_presentations/tests/src/DrupalTestTraits/Entity/PresentationTest.php +++ b/modules/opd_presentations/tests/src/DrupalTestTraits/Entity/PresentationTest.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace Drupal\opd_presentations\DrupalTestTraits\Entity; -use Drupal\paragraphs\Entity\Paragraph; +use Drupal\Core\Datetime\DrupalDateTime; use Drupal\opd_presentations\Entity\Presentation; +use Drupal\paragraphs\Entity\Paragraph; +use Drupal\paragraphs\ParagraphInterface; use weitzman\DrupalTestTraits\ExistingSiteBase; final class PresentationTest extends ExistingSiteBase { @@ -13,7 +15,9 @@ final class PresentationTest extends ExistingSiteBase { public function test_getting_past_events(): void { $presentation = $this->createPresentation( events: [ - Paragraph::create(['type' => 'event']), + $this->createEvent(['field_date' => (new DrupalDateTime('today'))->format('U')]), + $this->createEvent(['field_date' => (new DrupalDateTime('yesterday'))->format('U')]), + $this->createEvent(['field_date' => (new DrupalDateTime('tomorrow'))->format('U')]), ], ); @@ -27,9 +31,24 @@ final class PresentationTest extends ExistingSiteBase { * @param ParagraphInterface[] $events */ private function createPresentation(array $events): Presentation { - return $this->createNode([ + $presentation = $this->createNode([ 'field_events' => $events, 'type' => 'presentation', ]); + + assert($presentation instanceof Presentation); + + return $presentation; } + + /** + * @param array $values + */ + private function createEvent(array $values = []): ParagraphInterface { + return Paragraph::create(array_merge( + ['type' => 'event'], + $values, + )); + } + }