build(deps): remove discoverable_entity_bundle_classes
As this module is no longer supported, remove it from the codebase and update all references to it within the custom code - instead manually wrapping nodes with the `Post` or `Talk` class, or returning it from a Repository. Fixes: #465
This commit is contained in:
parent
41e13fe078
commit
cae2091436
24 changed files with 214 additions and 150 deletions
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Drupal\opdavies_talks\Collection;
|
||||
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
|
|
@ -16,7 +17,8 @@ final class TalkCollection extends Collection {
|
|||
* @return Collection|ParagraphInterface[]
|
||||
*/
|
||||
public function getEvents(): Collection {
|
||||
return $this->flatMap(fn(Talk $talk): Collection => $talk->getEvents());
|
||||
return $this
|
||||
->flatMap(fn(Talk $talk): Collection => $talk->getEvents());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,27 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_talks\Entity\Node;
|
||||
|
||||
use Drupal\discoverable_entity_bundle_classes\ContentEntityBundleInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Defines an talk node class.
|
||||
*
|
||||
* @ContentEntityBundleClass(
|
||||
* label = @Translation("Talk"),
|
||||
* entity_type = "node",
|
||||
* bundle = "talk"
|
||||
* );
|
||||
*/
|
||||
class Talk extends Node implements ContentEntityBundleInterface {
|
||||
final class Talk {
|
||||
|
||||
public const FIELD_EVENTS = 'field_events';
|
||||
public const FIELD_EVENT_DATE = 'field_event_date';
|
||||
|
||||
private NodeInterface $node;
|
||||
|
||||
public function __construct(EntityInterface $node) {
|
||||
$this->node = $node;
|
||||
}
|
||||
|
||||
public function addEvent(ParagraphInterface $event): void {
|
||||
$this->set(
|
||||
self::FIELD_EVENTS,
|
||||
|
|
@ -28,6 +29,26 @@ class Talk extends Node implements ContentEntityBundleInterface {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the date for the latest event.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function findLatestEventDate(): ?string {
|
||||
return $this->getEvents()
|
||||
->map(fn(ParagraphInterface $event) => $event->get('field_date')
|
||||
->getString())
|
||||
->max();
|
||||
}
|
||||
|
||||
public function get(string $name): FieldItemListInterface {
|
||||
return $this->node->get($name);
|
||||
}
|
||||
|
||||
public function getCreatedTime(): int {
|
||||
return (int) $this->node->getCreatedTime();
|
||||
}
|
||||
|
||||
public function getEvents(): Collection {
|
||||
return Collection::make($this->get(self::FIELD_EVENTS)
|
||||
->referencedEntities());
|
||||
|
|
@ -41,16 +62,24 @@ class Talk extends Node implements ContentEntityBundleInterface {
|
|||
return (int) $this->get(self::FIELD_EVENT_DATE)->getString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the date for the latest event.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function findLatestEventDate(): ?string {
|
||||
return $this->getEvents()
|
||||
->map(fn(ParagraphInterface $event) => $event->get('field_date')
|
||||
->getString())
|
||||
->max();
|
||||
public function id(): int {
|
||||
return (int) $this->node->id();
|
||||
}
|
||||
|
||||
public function label(): string {
|
||||
return $this->node->label();
|
||||
}
|
||||
|
||||
public function save(): void {
|
||||
$this->node->save();
|
||||
}
|
||||
|
||||
public function set(string $name, $value): void {
|
||||
$this->node->set($name, $value);
|
||||
}
|
||||
|
||||
public function setCreatedTime(int $timestamp): void {
|
||||
$this->node->setCreatedTime($timestamp);
|
||||
}
|
||||
|
||||
public function setEvents(array $events): void {
|
||||
|
|
@ -61,4 +90,9 @@ class Talk extends Node implements ContentEntityBundleInterface {
|
|||
$this->set(self::FIELD_EVENT_DATE, $date);
|
||||
}
|
||||
|
||||
public static function createFromNode(EntityInterface $node): self {
|
||||
// TODO: ensure that this is a node and a `talk` type.
|
||||
return new self($node);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,8 +32,9 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
|
|||
return;
|
||||
}
|
||||
|
||||
/** @var Talk $talk */
|
||||
$talk = $event->getEntity();
|
||||
$node = $event->getEntity();
|
||||
$talk = Talk::createFromNode($node);
|
||||
|
||||
$this->reorderEvents($talk);
|
||||
$this->updateCreatedDate($talk);
|
||||
}
|
||||
|
|
@ -42,9 +43,7 @@ final class UpdateTalkNodeBeforeSave implements EventSubscriberInterface {
|
|||
$events = $talk->getEvents();
|
||||
$eventsByDate = $this->sortEventsByDate($events);
|
||||
|
||||
// If the original event IDs don't match the sorted event IDs, update the
|
||||
// event field to use the sorted ones.
|
||||
// @phpstan-ignore-next-line
|
||||
// If the original event IDs don't match the sorted event IDs, update the event field to use the sorted ones.
|
||||
if ($events->map->id() != $eventsByDate->map->id()) {
|
||||
$talk->setEvents($eventsByDate->toArray());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityStorageInterface;
|
|||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opdavies_talks\Collection\TalkCollection;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
|
||||
final class TalkRepository {
|
||||
|
||||
|
|
@ -20,7 +21,8 @@ final class TalkRepository {
|
|||
public function findAll(): TalkCollection {
|
||||
$talks = $this->nodeStorage->loadByProperties($this->defaultProperties());
|
||||
|
||||
return new TalkCollection($talks);
|
||||
return (new TalkCollection($talks))
|
||||
->map(fn(NodeInterface $node): Talk => Talk::createFromNode($node));
|
||||
}
|
||||
|
||||
public function findAllPublished(): TalkCollection {
|
||||
|
|
@ -31,7 +33,8 @@ final class TalkRepository {
|
|||
],
|
||||
));
|
||||
|
||||
return new TalkCollection($talks);
|
||||
return (new TalkCollection($talks))
|
||||
->map(fn(NodeInterface $node): Talk => Talk::createFromNode($node));
|
||||
}
|
||||
|
||||
private function defaultProperties(): array {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue