Add an Event bundle class
This commit is contained in:
parent
f56eb931c2
commit
14cd79a960
6 changed files with 49 additions and 14 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ParagraphInterface>
|
||||
* @implements \IteratorAggregate<Event>
|
||||
*/
|
||||
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];
|
||||
}
|
||||
|
||||
|
|
24
modules/opd_presentations/src/Entity/Event.php
Normal file
24
modules/opd_presentations/src/Entity/Event.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations\Entity;
|
||||
|
||||
use Drupal\paragraphs\Entity\Paragraph;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
|
||||
final class Event extends Paragraph implements ParagraphInterface {
|
||||
|
||||
public const PARAGRAPH_TYPE = 'event';
|
||||
|
||||
public function getEventDate(): string {
|
||||
/** @var non-empty-string */
|
||||
return $this->get('field_date')->value;
|
||||
}
|
||||
|
||||
public function getEventName(): string {
|
||||
/** @var non-empty-string */
|
||||
return $this->get('field_event_name')->value;
|
||||
}
|
||||
|
||||
}
|
|
@ -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,
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
@ -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',
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue