Get past events for each presentation

This commit is contained in:
Oliver Davies 2025-06-10 20:47:23 +01:00
parent 122f4a9bec
commit 6e19d01eed
2 changed files with 32 additions and 4 deletions

View file

@ -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,
);
}
}

View file

@ -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<non-empty-string, mixed> $values
*/
private function createEvent(array $values = []): ParagraphInterface {
return Paragraph::create(array_merge(
['type' => 'event'],
$values,
));
}
}