Get past events for each presentation
This commit is contained in:
parent
122f4a9bec
commit
6e19d01eed
2 changed files with 32 additions and 4 deletions
|
@ -4,13 +4,22 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Drupal\opd_presentations\Entity;
|
namespace Drupal\opd_presentations\Entity;
|
||||||
|
|
||||||
|
use Drupal\Core\Datetime\DrupalDateTime;
|
||||||
use Drupal\node\Entity\Node;
|
use Drupal\node\Entity\Node;
|
||||||
use Drupal\node\NodeInterface;
|
use Drupal\node\NodeInterface;
|
||||||
|
use Drupal\paragraphs\Entity\Paragraph;
|
||||||
|
|
||||||
final class Presentation extends Node implements NodeInterface {
|
final class Presentation extends Node implements NodeInterface {
|
||||||
|
|
||||||
public function getPastEvents(): array {
|
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,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,10 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Drupal\opd_presentations\DrupalTestTraits\Entity;
|
namespace Drupal\opd_presentations\DrupalTestTraits\Entity;
|
||||||
|
|
||||||
use Drupal\paragraphs\Entity\Paragraph;
|
use Drupal\Core\Datetime\DrupalDateTime;
|
||||||
use Drupal\opd_presentations\Entity\Presentation;
|
use Drupal\opd_presentations\Entity\Presentation;
|
||||||
|
use Drupal\paragraphs\Entity\Paragraph;
|
||||||
|
use Drupal\paragraphs\ParagraphInterface;
|
||||||
use weitzman\DrupalTestTraits\ExistingSiteBase;
|
use weitzman\DrupalTestTraits\ExistingSiteBase;
|
||||||
|
|
||||||
final class PresentationTest extends ExistingSiteBase {
|
final class PresentationTest extends ExistingSiteBase {
|
||||||
|
@ -13,7 +15,9 @@ final class PresentationTest extends ExistingSiteBase {
|
||||||
public function test_getting_past_events(): void {
|
public function test_getting_past_events(): void {
|
||||||
$presentation = $this->createPresentation(
|
$presentation = $this->createPresentation(
|
||||||
events: [
|
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
|
* @param ParagraphInterface[] $events
|
||||||
*/
|
*/
|
||||||
private function createPresentation(array $events): Presentation {
|
private function createPresentation(array $events): Presentation {
|
||||||
return $this->createNode([
|
$presentation = $this->createNode([
|
||||||
'field_events' => $events,
|
'field_events' => $events,
|
||||||
'type' => 'presentation',
|
'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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue