Return an EventCollection
This commit is contained in:
parent
6e19d01eed
commit
8d8bf62f2c
2 changed files with 28 additions and 3 deletions
24
modules/opd_presentations/src/Collection/EventCollection.php
Normal file
24
modules/opd_presentations/src/Collection/EventCollection.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations\Collection;
|
||||
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
|
||||
/**
|
||||
* @implements \IteratorAggregate<ParagraphInterface>
|
||||
*/
|
||||
readonly final class EventCollection implements \IteratorAggregate {
|
||||
|
||||
/**
|
||||
* @param ParagraphInterface[] $events
|
||||
*/
|
||||
public function __construct(private array $events) {
|
||||
}
|
||||
|
||||
public function getIterator(): \Traversable {
|
||||
return new \ArrayIterator($this->events);
|
||||
}
|
||||
|
||||
}
|
|
@ -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,
|
||||
);
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue