Count past presentations
This commit is contained in:
parent
820f8d4055
commit
92c413bc80
6 changed files with 143 additions and 6 deletions
|
@ -9,7 +9,11 @@ use Webmozart\Assert\Assert;
|
|||
/**
|
||||
* @implements \IteratorAggregate<Event>
|
||||
*/
|
||||
readonly final class Events implements \IteratorAggregate {
|
||||
readonly final class Events implements \Countable, \IteratorAggregate {
|
||||
|
||||
public function count(): int {
|
||||
return count($this->events);
|
||||
}
|
||||
|
||||
public function filter(\Closure $callback): self {
|
||||
return new self(array_filter(
|
||||
|
|
|
@ -11,9 +11,8 @@ final class Presentation extends Node implements NodeInterface {
|
|||
|
||||
public const NODE_TYPE = 'presentation';
|
||||
|
||||
public function getPastEvents(): Events {
|
||||
return Events::fromEvents($this->get('field_events')->referencedEntities())
|
||||
->getPast();
|
||||
public function getEvents(): Events {
|
||||
return Events::fromEvents($this->get('field_events')->referencedEntities());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
37
modules/opd_presentations/src/PresentationCounter.php
Normal file
37
modules/opd_presentations/src/PresentationCounter.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
final class PresentationCounter {
|
||||
|
||||
private array $presentations;
|
||||
|
||||
public function __construct(private EntityTypeManagerInterface $entityTypeManager) {
|
||||
}
|
||||
|
||||
public function getPastCount(): int {
|
||||
$nodeStorage = $this->entityTypeManager->getStorage('node');
|
||||
|
||||
$query = $nodeStorage->getQuery();
|
||||
$query->condition('status', NodeInterface::PUBLISHED);
|
||||
$query->condition('type', Presentation::NODE_TYPE);
|
||||
$query->accessCheck();
|
||||
|
||||
$nodeIds = $query->execute();
|
||||
|
||||
$presentations = $nodeStorage->loadMultiple($nodeIds);
|
||||
|
||||
return array_reduce(
|
||||
array: $presentations,
|
||||
callback: fn (int $count, Presentation $presentation)
|
||||
=> $count + $presentation->getEvents()->getPast()->count(),
|
||||
initial: 0,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue