From 14cd79a9606501ebeddabbc8de4968b588d6710f Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 12 Jun 2025 02:10:08 +0100 Subject: [PATCH] Add an Event bundle class --- .../opd_presentations.module | 5 ++++ .../src/Collection/EventCollection.php | 8 +++---- .../opd_presentations/src/Entity/Event.php | 24 +++++++++++++++++++ .../src/Entity/Presentation.php | 4 ++-- .../Functional/Entity/PresentationTest.php | 2 +- .../src/Traits/PresentationCreationTrait.php | 20 ++++++++++------ 6 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 modules/opd_presentations/src/Entity/Event.php diff --git a/modules/opd_presentations/opd_presentations.module b/modules/opd_presentations/opd_presentations.module index d8662e67d..d1244ef41 100644 --- a/modules/opd_presentations/opd_presentations.module +++ b/modules/opd_presentations/opd_presentations.module @@ -2,6 +2,7 @@ declare(strict_types=1); +use Drupal\opd_presentations\Entity\Event; use Drupal\opd_presentations\Entity\Presentation; /** @@ -13,4 +14,8 @@ function opd_presentations_entity_bundle_info_alter(array &$bundles): void { if (isset($bundles['node'])) { $bundles['node']['presentation']['class'] = Presentation::class; } + + if (isset($bundles['paragraph'])) { + $bundles['paragraph'][Event::PARAGRAPH_TYPE]['class'] = Event::class; + } } diff --git a/modules/opd_presentations/src/Collection/EventCollection.php b/modules/opd_presentations/src/Collection/EventCollection.php index 3cbf64a8e..f1f271489 100644 --- a/modules/opd_presentations/src/Collection/EventCollection.php +++ b/modules/opd_presentations/src/Collection/EventCollection.php @@ -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 + * @implements \IteratorAggregate */ 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]; } diff --git a/modules/opd_presentations/src/Entity/Event.php b/modules/opd_presentations/src/Entity/Event.php new file mode 100644 index 000000000..cedd43c86 --- /dev/null +++ b/modules/opd_presentations/src/Entity/Event.php @@ -0,0 +1,24 @@ +get('field_date')->value; + } + + public function getEventName(): string { + /** @var non-empty-string */ + return $this->get('field_event_name')->value; + } + +} diff --git a/modules/opd_presentations/src/Entity/Presentation.php b/modules/opd_presentations/src/Entity/Presentation.php index da7ccde6c..ba5b91724 100644 --- a/modules/opd_presentations/src/Entity/Presentation.php +++ b/modules/opd_presentations/src/Entity/Presentation.php @@ -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, )); } diff --git a/modules/opd_presentations/tests/src/Functional/Entity/PresentationTest.php b/modules/opd_presentations/tests/src/Functional/Entity/PresentationTest.php index c28902337..0bc319957 100644 --- a/modules/opd_presentations/tests/src/Functional/Entity/PresentationTest.php +++ b/modules/opd_presentations/tests/src/Functional/Entity/PresentationTest.php @@ -39,7 +39,7 @@ final class PresentationTest extends ExistingSiteBase { ); $this->assertSame( - actual: $events->first()->get('field_event_name')->value, + actual: $events->first()->getEventName(), expected: 'yesterday', ); } diff --git a/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php b/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php index 605309c07..452e1db4f 100644 --- a/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php +++ b/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php @@ -6,16 +6,17 @@ namespace Drupal\Tests\opd_presentations\Traits; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Tests\node\Traits\NodeCreationTrait; +use Drupal\ctools\Testing\EntityCreationTrait; +use Drupal\opd_presentations\Entity\Event; use Drupal\opd_presentations\Entity\Presentation; -use Drupal\paragraphs\Entity\Paragraph; -use Drupal\paragraphs\ParagraphInterface; trait PresentationCreationTrait { + use EntityCreationTrait; use NodeCreationTrait; /** - * @param ParagraphInterface[] $events + * @param Event[] $events */ private function createPresentation(array $events): Presentation { $presentation = $this->createNode([ @@ -28,14 +29,19 @@ trait PresentationCreationTrait { return $presentation; } - private function createEvent(string $eventName, string $eventDate): ParagraphInterface { - return Paragraph::create( - [ + private function createEvent(string $eventName, string $eventDate): Event { + $event = $this->createEntity( + entity_type: 'paragraph', + values: [ 'field_date' => (new DrupalDateTime($eventDate))->getTimestamp(), 'field_event_name' => $eventName, - 'type' => 'event', + 'type' => Event::PARAGRAPH_TYPE, ], ); + + assert($event instanceof Event); + + return $event; } }